在 Linux 中使用 pushd 和 popd 進(jìn)行高效的文件目錄導(dǎo)航
有時(shí),使用命令瀏覽 Linux 文件系統(tǒng)可能會(huì)很痛苦,尤其是對(duì)于新手而言。通常,我們主要使用cd(更改目錄)命令來(lái)移動(dòng) Linux 文件系統(tǒng)。
本教程將解釋一組Linux命令:“ pushd ”和“ popd ”,它們用于高效導(dǎo)航 Linux 目錄結(jié)構(gòu)。它們存在于大多數(shù) shell 中,例如 bash、tcsh 等。
在 Linux 中pushd 和 popd 命令是如何工作的
pushd和popd根據(jù)“ LIFO ”(后進(jìn)先出)原則工作。在這個(gè)原則中,只允許兩種操作:將一個(gè)項(xiàng)目壓入堆棧,以及從堆棧中彈出一個(gè)項(xiàng)目。
pushd 在棧頂添加一個(gè)目錄,popd 從棧頂刪除一個(gè)目錄。
要顯示目錄堆棧(或歷史記錄)中的目錄,我們可以使用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 命令- 將目錄路徑放入/添加到目錄堆棧(歷史記錄)中,稍后允許您導(dǎo)航回歷史記錄中的任何目錄。當(dāng)您將目錄添加到堆棧時(shí),它也會(huì)回顯歷史(或“堆棧”)中存在的內(nèi)容。
這些命令顯示了 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 - 將目錄添加到堆棧
從上面輸出中的目錄堆棧可以看到(目錄索引順序相反):
- [linuxmi@localhost /test]$dirs -v
- 0 /test 是目錄堆棧中的第五個(gè) [索引為 0]。
- 1 /mnt/hgfs 是目錄堆棧中的第四個(gè) [索引為 1]。
- 2 ~/web/wp-admin 是目錄堆棧中的第三個(gè) [索引為 2]。
- 3 /var/www/html 是目錄堆棧中的第二個(gè) [索引為 3]。
- 4 ~/www.linuxmi.com 是目錄堆棧中的第一個(gè) [索引為 1]。
或者,我們可以使用表單中的目錄索引pushd +# 或 pushd -#將目錄添加到堆棧中。要進(jìn)入/var/www/html,我們將輸入:
- [linuxmi@localhost /test]$pushd +3
- /var/www/html ~/www.linuxmi.com /test /mnt/hgfs ~/web/wp-admin
注意在此之后,堆棧內(nèi)容將發(fā)生變化。因此,從前面的示例中,要進(jìn)入~/www.linuxmi.com,我們將使用:
- [linuxmi@localhost /var/www/html]$pushd +1
- ~/www.linuxmi.com /test /mnt/hgfs ~/web/wp-admin /var/www/html
pushd - 用號(hào)碼導(dǎo)航目錄
popd 命令——從堆?;驓v史記錄的頂部刪除一個(gè)目錄。要列出目錄堆棧,請(qǐng)鍵入:
- [linuxmi@localhost ~/www.linuxmi.com]$popd
- /test /mnt/hgfs ~/web/wp-admin /var/www/html
要從目錄堆棧中刪除目錄,請(qǐng)使用popd +# 或 popd -#,在這種情況下,我們將鍵入以下命令以刪除/mnt/hgfs:
- [linuxmi@localhost /test]$popd +1
- /test ~/web/wp-admin /var/www/html
popd – 從堆棧中刪除目錄
在本教程中,我們解釋了用于導(dǎo)航目錄結(jié)構(gòu)的“pushd ”和“ popd ”命令。