在 Linux 中使用 pushd 和 popd 進行高效的文件目錄導航
有時,使用命令瀏覽 Linux 文件系統可能會很痛苦,尤其是對于新手而言。通常,我們主要使用cd(更改目錄)命令來移動 Linux 文件系統。
本教程將解釋一組Linux命令:“ pushd ”和“ popd ”,它們用于高效導航 Linux 目錄結構。它們存在于大多數 shell 中,例如 bash、tcsh 等。
在 Linux 中pushd 和 popd 命令是如何工作的
pushd和popd根據“ LIFO ”(后進先出)原則工作。在這個原則中,只允許兩種操作:將一個項目壓入堆棧,以及從堆棧中彈出一個項目。
pushd 在棧頂添加一個目錄,popd 從棧頂刪除一個目錄。
要顯示目錄堆棧(或歷史記錄)中的目錄,我們可以使用dirs命令,如圖所示。
- [linuxmi@localhost ~/www.linuxmi.com/linuxmi]$dirs
- ~/www.linuxmi.com/linuxmi
- 或者
- [linuxmi@localhost ~/www.linuxmi.com/linuxmi]$dirs -v
- 0 ~/www.linuxmi.com/linuxmi
pushd 命令- 將目錄路徑放入/添加到目錄堆棧(歷史記錄)中,稍后允許您導航回歷史記錄中的任何目錄。當您將目錄添加到堆棧時,它也會回顯歷史(或“堆棧”)中存在的內容。
這些命令顯示了 pushd 的工作原理:
- [linuxmi@localhost ~/www.linuxmi.com]$pushd /var/www/html/
- /var/www/html ~/www.linuxmi.com
- [linuxmi@localhost /var/www/html]$pushd /home/linuxmi/web/wp-admin
- ~/web/wp-admin /var/www/html ~/www.linuxmi.com
- [linuxmi@localhost ~/web/wp-admin]$pushd /mnt/hgfs
- /mnt/hgfs ~/web/wp-admin /var/www/html ~/www.linuxmi.com
- [linuxmi@localhost /mnt/hgfs]$pushd /test
- /test /mnt/hgfs ~/web/wp-admin /var/www/html ~/www.linuxmi.com
pushd - 將目錄添加到堆棧
從上面輸出中的目錄堆??梢钥吹剑夸浰饕樞蛳喾矗?nbsp;
- [linuxmi@localhost /test]$dirs -v
- 0 /test 是目錄堆棧中的第五個 [索引為 0]。
- 1 /mnt/hgfs 是目錄堆棧中的第四個 [索引為 1]。
- 2 ~/web/wp-admin 是目錄堆棧中的第三個 [索引為 2]。
- 3 /var/www/html 是目錄堆棧中的第二個 [索引為 3]。
- 4 ~/www.linuxmi.com 是目錄堆棧中的第一個 [索引為 1]。
或者,我們可以使用表單中的目錄索引pushd +# 或 pushd -#將目錄添加到堆棧中。要進入/var/www/html,我們將輸入:
- [linuxmi@localhost /test]$pushd +3
- /var/www/html ~/www.linuxmi.com /test /mnt/hgfs ~/web/wp-admin
注意在此之后,堆棧內容將發生變化。因此,從前面的示例中,要進入~/www.linuxmi.com,我們將使用:
- [linuxmi@localhost /var/www/html]$pushd +1
- ~/www.linuxmi.com /test /mnt/hgfs ~/web/wp-admin /var/www/html
pushd - 用號碼導航目錄
popd 命令——從堆?;驓v史記錄的頂部刪除一個目錄。要列出目錄堆棧,請鍵入:
- [linuxmi@localhost ~/www.linuxmi.com]$popd
- /test /mnt/hgfs ~/web/wp-admin /var/www/html
要從目錄堆棧中刪除目錄,請使用popd +# 或 popd -#,在這種情況下,我們將鍵入以下命令以刪除/mnt/hgfs:
- [linuxmi@localhost /test]$popd +1
- /test ~/web/wp-admin /var/www/html
popd – 從堆棧中刪除目錄
在本教程中,我們解釋了用于導航目錄結構的“pushd ”和“ popd ”命令。