Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

目錄堆疊指令 pushd 與 popd 用法詳解

Linux 的目錄堆疊用於暫存工作目錄,方便使用者手動在多個路徑間切換。Bash 定義了 pushd 與 popd 兩個指令來維護這個堆疊,而 dirs 則用來顯示目前堆疊內容,$DIRSTACK 變數可取得堆疊頂端的值。

以下為一組操作示範:

[root@nn home]# pushd /home/
/home /home /home/zhangna/learn_test
[root@nn home]# dirs
/home /home /home/zhangna/learn_test
[root@nn home]# pushd /root
~ /home /home /home/zhangna/learn_test
[root@nn ~]# pushd /etc
/etc ~ /home /home /home/zhangna/learn_test
[root@nn etc]# dirs
/etc ~ /home /home /home/zhangna/learn_test
[root@nn etc]# echo $DIRSTACK
/etc
[root@nn etc]# popd
~ /home /home /home/zhangna/learn_test
[root@nn ~]# popd
/home /home /home/zhangna/learn_test
[root@nn home]# popd
/home /home/zhangna/learn_test
[root@nn home]# popd
/home/zhangna/learn_test
[root@nn learn_test]# popd
bash: popd: directory stack empty

三個指令的作用整理如下:

pushd:將指定目錄壓入堆疊,並切換到該目錄。

popd:將堆疊頂端的目錄彈出,並切換回去。

dirs:顯示目前目錄堆疊的所有內容。

當堆疊已空時再執行 popd,便會出現 directory stack empty 的提示。

Copyright © Linux教程網 All Rights Reserved