Home > Article > System Tutorial > Learn to use pushd and popd commands to improve browsing efficiency of Linux directory structure
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. |
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.
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
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/
pushd-Add a new directory to the stack
According to the directory stack output above (the directory index is arranged in reverse order):
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
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
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
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!