Home  >  Article  >  System Tutorial  >  Learn to use pushd and popd commands to improve browsing efficiency of Linux directory structure

Learn to use pushd and popd commands to improve browsing efficiency of Linux directory structure

王林
王林forward
2023-12-30 09:19:13615browse
Introduction Sometimes, navigating the Linux file system through commands can be a very painful thing, especially for newbies. Normally, we mainly use the cd (change directory) command to move between Linux file systems.

高效的浏览 Linux 目录结构:pushd和popd命令
In this tutorial, we will discuss two related commands: pushd and popd, which can be used to efficiently browse the Linux directory structure. These two commands exist in most shells, such as bash and tcsh.

How pushd and popd commands work in Linux systems

The pushd and popd commands work according to the ‘LIFO’ (last in, first out) principle. Under this principle, only two operations are allowed: pushing a directory onto the stack, and popping a directory off the stack.

The pushd command will add a directory to the top of the stack, and the popd command will remove a directory from the top of the stack.

In order to display the directories in the directory stack (or history), we can use the dirs command shown below:

$ dirs
或
$ dirs -v

高效的浏览 Linux 目录结构:pushd和popd命令

dirs - Display directories located in the directory stack

pushd command: Add/put a directory path to/from the directory stack (history). After that, you can browse any directory located in the directory stack (history). When a new directory is pushed onto the stack, all directories currently on the stack are printed.

The following commands will show how this command works:

$ pushd  /var/www/html/
$ pushd ~/Documents/
$ pushd ~/Desktop/
$ pushd /var/log/

高效的浏览 Linux 目录结构:pushd和popd命令

pushd-Add a new directory to the stack

According to the directory stack output above (the directory index is arranged in reverse order):

  • /var/log is the fifth directory in the directory stack, with index 0
  • ~/Desktop/ is the fourth one with index 1
  • ~/Document/ is the third one with index 2
  • /var/www/html is the second one, with index 3
  • ~ is the first one, with index 4

In addition, we can also use the directory index form pushd # or pushd -# to add directories to the stack. In order to enter the directory ~/Documents, we can enter:

$ pushd +2

高效的浏览 Linux 目录结构:pushd和popd命令

pushd - Browse Directory by Number

Note that after the previous step, the contents of the stack have changed. So, to go to the directory /var/www/html from the above example, we should use the following command:

$ pushd +1

高效的浏览 Linux 目录结构:pushd和popd命令

pushd - Browse Directory by Number

popd command - removes a directory from the top of the stack or history. To list all directories in the directory stack, just type:

$ popd

In order to remove a directory from the directory stack, we can use the popd # or popd -# command. At this time, we need to enter the following command to remove the directory ~/Documents:

$ popd +1

高效的浏览 Linux 目录结构:pushd和popd命令

popd - Remove directory from stack


The above is the detailed content of Learn to use pushd and popd commands to improve browsing efficiency of Linux directory structure. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:linuxprobe.com. If there is any infringement, please contact admin@php.cn delete