Table of Contents
This tip is about the how to Get started with pushd and popd on Linux. So read this free guide, How to Get started with pushd and popd on Linux step by step. If you have query related to same article you may contact us.
How to Get started with pushd and popd on Linux – Guide
On Linux and other Unix-like operating systems, the pushd and popd commands allow you to interact with directory stacks and modify the current working directory. Despite the fact that pushd and popd are extremely strong and useful commands, they are underrated and underused. Navigating the Linux file system using commands can be difficult at times, especially for newcomers. Normally, we move the Linux file system with the cd (Change Directory) command. we will teach you how to navigate your system directory tree with the pushd and popd commands in this article.
How pushd and popd commands work on Linux
pushd and popd work according to the “LIFO” (last in, first out) principle. In this principle, only two operations are allowed: putting an item on the stack and taking 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 directories in the directory stack (or history) we can use the dirs command as shown.
$dirs OR $dirs -v
pushd command – puts/adds directory paths in a directory stack (history) and then allows you to navigate back to any directory in history. As you add directories to the stack, it also echoes what exists in the history (or “stack”).
The commands show how pushd works:
$pushd /var/www/html/ $pushd ~/Documents/ $pushd ~/Desktop/ $pushd /var/log/
Optionally, we can use the directory index in the form pushd +# or pushd -# to add directories to the stack. To go to ~/Documents, we would type:
$ pushed +2
Note that after that, the contents of the stack will change. So from the previous example, to go to /var/www/html we would use:
$ pressed +1
popd command – removes a directory from the top of the stack or history. To list the directory stack, type:
$ popd
To remove a directory from the directory stack, use popd +# or popd -#, in this case we would type the command below to remove ~/Documents:
$ popd +1
Final note
I hope you like the guide How to Get started with pushd and popd on Linux. In case if you have any query regards this article you may ask us. Also, please share your love by sharing this article with your friends.