Use pushd and popd for efficient file directory navigation in Linux
Sometimes, browsing Linux file systems using commands can be a pain, especially for newbies. Usually, we mainly use the cd (change directory) command to move Linux file systems.
This tutorial will explain a set of Linux commands: " pushd " and " popd ", which are used to navigate the Linux directory structure efficiently. They are present in most shells, such as bash, tcsh, etc.
How pushd and popd commands work in Linux
pushd and popd work according to the "LIFO" (last in, first out) principle. In this principle, only two operations are allowed: pushing an item onto the stack, and popping an item off the stack.
pushd adds a directory to the top of the stack, and popd removes a directory from the top of the stack.
To display the directories in the directory stack (or history), we can use the dirs command as shown in the image.
[linuxmi@localhost ~/www.linuxmi.com/linuxmi]$dirs
~/www.linuxmi.com/linuxmi
or
[linuxmi@localhost ~/www.linuxmi.com/linuxmi]$dirs -v
0 ~/www.linuxmi.com/linuxmi
pushd - Add a directory to the stack
pushd command - puts/adds a directory path to the directory stack (history), later allowing you to navigate back to any directory in the history. When you add a directory to the stack, it also echoes what exists in the history (or "stack").
These commands show how pushd works:
[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
As you can see from the directory stack in the output above (directory index order is reversed):
[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 - Navigate directories by number
We can add a directory to the stack using the directory index in the form pushd +# or pushd -#. To get to /var/www/html we would type:
[linuxmi@localhost /test]$pushd +3
/var/www/html ~/www.linuxmi.com /test /mnt/hgfs ~/web/wp-admin
Note that after this, the stack contents will change. So, from the previous example, to go to ~/www.linuxmi.com we would use:
[linuxmi@localhost /var/www/html]$pushd +1
~/www.linuxmi.com /test /mnt/hgfs ~/web/wp-admin /var/www/html
popd – Remove a directory from the stack
popd command - removes a directory from the top of the stack or history. To list the directory stack, type:
[linuxmi@localhost ~/www.linuxmi.com]$popd
/test /mnt/hgfs ~/web/wp-admin /var/www/html
To remove a directory from the directory stack, use popd +# or popd -#, in this case we will type the following command to remove /mnt/hgfs:
[linuxmi@localhost /test]$popd +1
/test ~/web/wp-admin /var/www/html
/test]$popd +1
/test ~/web/wp-admin /var/www/html
[外链图片转存中...(img-8wFhkgKA-1680253320368)]