在 Linux 中使用 pushd 和 popd 进行高效的文件目录导航

系统 Linux
本教程将解释一组Linux命令:“ pushd ”和“ popd ”,它们用于高效导航 Linux 目录结构。它们存在于大多数 shell 中,例如 bash、tcsh 等。

 [[406787]]

有时,使用命令浏览 Linux 文件系统可能会很痛苦,尤其是对于新手而言。通常,我们主要使用cd(更改目录)命令来移动 Linux 文件系统。

本教程将解释一组Linux命令:“ pushd ”和“ popd ”,它们用于高效导航 Linux 目录结构。它们存在于大多数 shell 中,例如 bash、tcsh 等。

在 Linux 中pushd 和 popd 命令是如何工作的

pushd和popd根据“ LIFO ”(后进先出)原则工作。在这个原则中,只允许两种操作:将一个项目压入堆栈,以及从堆栈中弹出一个项目。

pushd 在栈顶添加一个目录,popd 从栈顶删除一个目录。

要显示目录堆栈(或历史记录)中的目录,我们可以使用dirs命令,如图所示。 

  1. [linuxmi@localhost ~/www.linuxmi.com/linuxmi]$dirs  
  2. ~/www.linuxmi.com/linuxmi  
  3. 或者  
  4. [linuxmi@localhost ~/www.linuxmi.com/linuxmi]$dirs -v 
  5.  0 ~/www.linuxmi.com/linuxmi 

 

pushd 命令- 将目录路径放入/添加到目录堆栈(历史记录)中,稍后允许您导航回历史记录中的任何目录。当您将目录添加到堆栈时,它也会回显历史(或“堆栈”)中存在的内容。

这些命令显示了 pushd 的工作原理: 

  1. [linuxmi@localhost ~/www.linuxmi.com]$pushd /var/www/html/  
  2. /var/www/html ~/www.linuxmi.com  
  3. [linuxmi@localhost /var/www/html]$pushd /home/linuxmi/web/wp-admin  
  4. ~/web/wp-admin /var/www/html ~/www.linuxmi.com  
  5. [linuxmi@localhost ~/web/wp-admin]$pushd /mnt/hgfs  
  6. /mnt/hgfs ~/web/wp-admin /var/www/html ~/www.linuxmi.com  
  7. [linuxmi@localhost /mnt/hgfs]$pushd /test  
  8. /test /mnt/hgfs ~/web/wp-admin /var/www/html ~/www.linuxmi.com 

pushd - 将目录添加到堆栈

从上面输出中的目录堆栈可以看到(目录索引顺序相反): 

  1. [linuxmi@localhost /test]$dirs -v  
  2. 0 /test  是目录堆栈中的第五个 [索引为 0]。  
  3. 1 /mnt/hgfs  是目录堆栈中的第四个 [索引为 1]。  
  4. 2 ~/web/wp-admin  是目录堆栈中的第三个 [索引为 2]。  
  5. 3 /var/www/html  是目录堆栈中的第二个 [索引为 3]。  
  6. 4 ~/www.linuxmi.com 是目录堆栈中的第一个 [索引为 1]。 

或者,我们可以使用表单中的目录索引pushd +# 或 pushd -#将目录添加到堆栈中。要进入/var/www/html,我们将输入: 

  1. [linuxmi@localhost /test]$pushd +3  
  2. /var/www/html ~/www.linuxmi.com /test /mnt/hgfs ~/web/wp-admin 

注意在此之后,堆栈内容将发生变化。因此,从前面的示例中,要进入~/www.linuxmi.com,我们将使用: 

  1. [linuxmi@localhost /var/www/html]$pushd +1  
  2. ~/www.linuxmi.com /test /mnt/hgfs ~/web/wp-admin /var/www/html 

pushd - 用号码导航目录

popd 命令——从堆栈或历史记录的顶部删除一个目录。要列出目录堆栈,请键入: 

  1. [linuxmi@localhost ~/www.linuxmi.com]$popd  
  2. /test /mnt/hgfs ~/web/wp-admin /var/www/html  

要从目录堆栈中删除目录,请使用popd +# 或 popd -#,在这种情况下,我们将键入以下命令以删除/mnt/hgfs: 

  1. [linuxmi@localhost /test]$popd +1  
  2. /test ~/web/wp-admin /var/www/html 

popd – 从堆栈中删除目录

在本教程中,我们解释了用于导航目录结构的“pushd ”和“ popd ”命令。 

 

责任编辑:庞桂玉 来源: Linux公社
相关推荐

2012-05-11 10:22:26

Linuxdirspushd

2009-07-31 17:21:28

linux cd命令linux cd命令详Pushd

2021-09-21 15:44:02

LinuxOBSWayland

2014-05-26 09:50:19

访问控制列表ACL文件保护

2009-10-27 13:10:49

linux cd命令

2009-12-14 13:33:31

linuxramdisk文件系统

2017-04-05 14:10:55

Linux命令行工具技巧

2021-12-12 09:19:16

find命令Linux

2009-02-27 17:15:05

XMLDOMXPath

2020-07-29 07:40:19

Linux系统Vim

2018-06-01 10:37:26

Linux归档文件压缩文件

2019-04-08 16:15:26

Linuxbd命令父目录

2012-05-09 10:38:37

2020-05-22 13:40:09

Linux文件系统

2021-11-29 22:59:34

Go Dockertest集成

2023-10-28 16:22:21

Go接口

2024-02-07 11:44:20

NestJSRxJS异步编程

2011-12-08 10:24:53

JavaNIO

2021-02-20 18:00:26

rangerLinux

2020-05-25 09:09:01

Linux件系统导航
点赞
收藏

51CTO技术栈公众号