The pushd command saves the current working directory in memory so it can be returned to at any time, optionally changing to a new directory.
The popd command returns to the path at the top of the directory stack. This directory stack is accessed by the command dirs
pushd
pushd [path | ..]
Arguments:
-
pathThis optional command-line argument specifies the directory to make the current directory. Ifpathis omitted, the path at the top of the directory stack is used, which has the effect of toggling between two directories
popd
popd
Examples
[test@dhcp-128-14 ~]$ dirs //查看当前目录堆栈dirs中有哪些目录 ~
[test@dhcp-128-14 ~]$ pushd ext/ ~/ext ~ [test@dhcp-128-14 ext]$ 切换到~/ext目录下
[test@dhcp-128-14 ext]$ dirs //pushd命令不仅将ext目录压入目录堆栈,并且实现了切换到ext目录
~/ext ~
..... 中间经过几次操作
[test@dhcp-128-14 ext1]$ dirs
~/ext/ext1/ext2 ~/ext/ext1 ~/ext ~
[test@dhcp-128-14 ext2]$ popd //popd命令会将目录堆栈顶端的元素弹出,并且切换到次顶端所在的目录,popd命令是不用带参数的
~/ext/ext1 ~/ext ~
[test@dhcp-128-14 ext1]$ 切换到~/ext/ext1目录下
[test@dhcp-128-14 ext1]$ dirs
~/ext/ext1 ~/ext ~
[yapei@dhcp-128-14 ext]$ dirs //当目录堆栈dirs中只有两个目录时,不带任何参数的pushd能够实现在两个目录间切换
~/ext ~
[yapei@dhcp-128-14 ext]$ pwd
/home/yapei/ext
[yapei@dhcp-128-14 ext]$ pushd
~ ~/ext
[yapei@dhcp-128-14 ~]$ 切换到~目录
[yapei@dhcp-128-14 ~]$ dirs
~ ~/ext
[yapei@dhcp-128-14 ~]$ pushd
~/ext ~
[yapei@dhcp-128-14 ext]$ 切换到~/ext目录
d[yapei@dhcp-128-14 ext]$ dirs
~/ext ~
本文详细介绍了Linux shell中的pushd和popd命令的使用方法。pushd命令可以保存当前工作目录并可选地更改为新目录,而popd命令则返回到目录堆栈顶部的路径。通过具体示例展示了如何利用这两个命令在多个目录间进行快速切换。
4258

被折叠的 条评论
为什么被折叠?



