Linux Commands Series 7 : Pushd and Popd commands
21/07/2020
In a previous tutorial, we learnt about the cd command. cd command is useful when you want to get somewhere in your operating systeme hierarchy. Now suppose you have many directories in mind that you would like to work in one by one, going to next after finishing work in the current directory : Two commands come to the rescue : pushd, and popd
pushd will push the directories provided as parameter to the directories stack one after an other. the last one added with pushd will be the working directory. popd will pop the top directory from the stack, making the terminal go the last directory added before the current one. Not very clear ? Let's showcase this with commands :
the syntax of popd is as follows :
pushd name_directory_here
Let's say we have somework to do in Downloads and Music folder, but first we want to start by myproject, so all we have is to push Downloads and Music to the stack, then myprject, hence myproject will be on top of the directory stack :
pushd Downloads/
Downloads is the current directory
pushd Music/
Music is the current directory
pushd myproject/
myproject is the current directory
At the top of the stack now, there is myproject direcotry, which will be the working directory :
the commands above shows every directory added to the directory stack becomes the current one.
When popd is executed (without any parameter) the command pops the current directory and goes to the previous one :
popd
my project is popped and Music becomes the current directory :
popd
Music is popped and Downloads becomes the current directory :
popd
==> will take you to the the first directory where you started
Now the stack is empty so if you try to execute it, it will alert you with the message :
bash: popd: directory stack empty
and you will stay on the same directory.
Notice that the content of the directory stack is displayed each time, with the top directory being the leftmost one
That was it for this tutorial, Now you have a good mechanism if you have many directories in mind that you want to work with one after an other, push with pushd, and pop with popd
I hope you learnt something, if you have any questions leave a comment, and if you found the post useful, don't forget to share it with your friends.
Happy Linux Learning !