不过,linux系统下还有可快捷的目录切换命令,pushd命令。
今天 我们就来学习下pushd命令的用法。
假设一种情况,一个文件A存放在了第十层目录ten中,进入ten,结果一不小心敲入了一个cd命令,看着光标在根目录下闪烁,是不是很无奈呢?
难道要再千辛万苦的敲一堆路径重新进到ten目录?
不必这么麻烦,输入“cd -”,看看是不是回到刚才的路径呢?
注:“cd -”能完成当前目录 和 之前所在的最近目录之间的切换
另外一个方便路径切换的命令就是pushd,可以完成多个不同路径之间的切换.
如果希望在/usr和/etc/ssh和/etc/rc.d和当前路径之间切换,则可以在命令行下输入:
root@jbxue:~$ pushd /usr
/usr ~
root@jbxue:/usr$ pushd /etc/ssh
/etc/ssh /usr ~
root@jbxue:/etc/ssh$ pushd /etc/rc.d
/etc/rc.d /etc/ssh /usr ~
pushd会自动把当前路径加入到可切换路径中。执行dirs,可以看到显示四个路径:
输入pushd,既可以在前两个路径之间相互切换。
如果想切换到最后一个(栈底)路径时 pushd +2即可。
root@jbxue:/etc/rc.d$ pushd
/etc/ssh /etc/rc.d /usr ~
root@jbxue:/etc/ssh$ pushd
/etc/rc.d /etc/ssh /usr ~
root@jbxue:/etc/rc.d$ pushd +2
/usr ~ /etc/rc.d /etc/ssh
如果想清空这些路径,执行dirs -c即可。
如果需要从堆栈中删除一个目录,键入popd,然后是目录名称,再按回车键。
附,pushd的命令使用说明。
Save and then change the current directory. With no arguments, pushd exchanges the top two directories.
SYNTAX
pushd [dir | +N | -N] [-n]
KEY
+N Brings the Nth directory (counting from the left of the list printed by dirs, starting with zero) to the top of the list by rotating the stack.
-N Brings the Nth directory (counting from the right of the list printed by dirs, starting with zero) to the top of the list by rotating the stack.
-n Suppresses the normal change of directory when adding directories to the stack, so that on
ly the stack is manipulated.
dir Makes the current working directory be the top of the stack, and then executes the equivalent of `cd dir'. cds to dir.
Related Linux Bash commands:
dirs - Display list of remembered directories
popd - Restore the previous value of the current directory saved by PUSHD