pushd

是将目录加入命令堆叠中。如果指令没有指定目录名称,则会将当前的工作目录置入目录堆叠的最顶端。置入目录如果没有指定堆叠的位置,也会置入目录堆叠的最顶端,同时工作目录会自动切换到目录堆叠最顶端的目录去。

语法

  pushd [dir | +N | -N] [-n]

选项

+N 删除从左到右的第n个目录,数字从0开始.

-N 删除从右到左的第n个目录,数字从0开始.

-n 只加入目录到堆叠中,不进行cd操作

dir 将当前工作目录设置为堆栈的顶部,执行等效于 `cd dir’

实例

Linux有三个关于目录切换的常用命令,分别是cd,pushd,popd
cd用于切换工作目录,但是如果你在两个目录之间需要频繁切换时,比如/etc/httpd/log/error和/home/testUser/Desktop/shell_test,那这样一直 cd cd会发疯的。所以pushd在此时是个好选择。

pushd的 功能是创建一个目录栈,你可以把你目前常见的几个目录压入栈中,并可以迅速地进行切换,非常方便。如



[root@localhost etc]# pushd ~/Desktop/shell_test/
~/Desktop/shell_test    /etc
[root@localhost shell_test]# 

可以看到我本来的工作目录是/etc,执行pushd ~/Desktop/shell_test/后将 ~/Desktop/shell_test/路径压入栈中,得到返回值。而且当前目录也跳转了

此时,再输入pushd会将栈顶目录和下一目录对调。即

[root@localhost shell_test]# pushd 
/etc    ~/Desktop/shell_test
[root@localhost etc]#

可以看到此时栈顶目录又变为/etc

至于popd,作用则是与pushd相反,将栈顶的目录弹出,此时除非再一次使用pushd压栈,否则pushd无法再找到已弹出的目录

[root@localhost etc]# popd
~/Desktop/shell_test
[root@localhost shell_test]#
-----
#popd [+N|-N]还可以指定目录栈的哪个目录优先出栈

建议:在使用pushd时,最好是在三两个目录频繁切换的场景下,否则目录栈变得过多就失去了pushd的优势了。
当然,在两个目录进行切换时,”cd -” 是一个不错的命令

pushd 命令实例:

linux命令:pushd /some/where/else; work; cd /somewhere; work; cd /another/place; popd popd /another/place; cd work; /somewhere; cd work; /some/where/else; pushd pushd /some/where/else; work; cd /somewhere; work; cd /another/place; popdrootopen.com
linux命令:pushd /some/where/else; work; cd /somewhere; work; cd /another/place; popd popd /another/place; cd work; /somewhere; cd work; /some/where/else; pushd pushd /some/where/else; work; cd /somewhere; work; cd /another/place; popdrootopen.com
2018-12-22 21:42:01

linux命令:pushd /tmp /tmp pushd pushd /tmprootopen.com
linux命令:pushd /tmp /tmp pushd pushd /tmprootopen.com
2017-12-29 17:30:48

linux命令:pushd +2; pushd -2 -2 pushd +2; pushd pushd +2; pushd -2rootopen.com
linux命令:pushd +2; pushd -2 -2 pushd +2; pushd pushd +2; pushd -2rootopen.com
2018-05-08 12:40:48

linux命令:pushd .> /dev/null; cd /; for d in `echo $OLDPWD | sed -e 's/\// /g'`; do cd $d; echo -n "$d "; ls -ld .; done; popd >/dev/null >/dev/null popd done; .; -ld ls "; "$d -n echo $d; cd do /g'`; 's/\// -e sed | $OLDPWD `echo in d for /; cd /dev/null; .> pushd pushd .> /dev/null; cd /; for d in `echo $OLDPWD | sed -e 's/\// /g'`; do cd $d; echo -n "$d "; ls -ld .; done; popd >/dev/nullrootopen.com
linux命令:pushd .> /dev/null; cd /; for d in `echo $OLDPWD | sed -e 's/\// /g'`; do cd $d; echo -n "$d "; ls -ld .; done; popd >/dev/null >/dev/null popd done; .; -ld ls "; "$d -n echo $d; cd do /g'`; 's/\// -e sed | $OLDPWD `echo in d for /; cd /dev/null; .> pushd pushd .> /dev/null; cd /; for d in `echo $OLDPWD | sed -e 's/\// /g'`; do cd $d; echo -n "$d "; ls -ld .; done; popd >/dev/nullrootopen.com
2018-01-02 14:52:48

linux命令:pushd /directory/to/remember /directory/to/remember pushd pushd /directory/to/rememberrootopen.com
linux命令:pushd /directory/to/remember /directory/to/remember pushd pushd /directory/to/rememberrootopen.com
2018-02-19 13:54:48

linux命令:pushd . . pushd pushd .rootopen.com
linux命令:pushd . . pushd pushd .rootopen.com
2018-05-26 14:54:48

linux命令:pushd path/to/dir/ path/to/dir/ pushd pushd path/to/dir/rootopen.com
linux命令:pushd path/to/dir/ path/to/dir/ pushd pushd path/to/dir/rootopen.com
2017-12-25 16:13:48

linux命令:pushd /path/to/dir ; command_to_execute; popd popd command_to_execute; ; /path/to/dir pushd pushd /path/to/dir ; command_to_execute; popdrootopen.com
linux命令:pushd /path/to/dir ; command_to_execute; popd popd command_to_execute; ; /path/to/dir pushd pushd /path/to/dir ; command_to_execute; popdrootopen.com
2018-01-06 16:22:48

Linux的Bash命令行(A-Z排序)