Every video, every podcast, blog post, etc. about being a productive Linux user, you will need to eventually master terminal. I have been using it for some time now and must admit, I do feel more productive. Sure, there is a learning curve involved, but hey, nowadays you need to learn and adapt to frequent changes to get things done.
Besides standard commands (grep, awk, sed, pipes and more), there are hidden gems, which are not that widely used. If you learn to harness their power, you can be unbelievable fast and productive. popd and pushd are one of those hidden treasures.
Last In, First Out?
pushd and popd are commands used to work with the command line directory stack in most of the OS (Windows – PowerShell is supported – cmdlet Push-Location). They enable you to easily traverse through different folders in a comprehensive and structured way. For developers it can be really useful. Imagine having different projects opened, with subfolders. Would’nt be nice to to add folders to the list and then jump between them with simply specifying position in the list?
That is what popd and pushd is all about. A quick, structured way to jump around your predefined folders.
If you want to check, what is the current directory stack, you can use dirs commands.
Commands outputs current stack and enables you to do more with elements.
pushd enables you to add directory to the stack (when you add to the stack, it outputs current content of the stack) and popd give you option to remove directory from the list. When you invoke pushd, it always store current working directory to the directory stack and then navigates to the entered path.
Easy to add directories to the stack
Let us play around a bit.
In order to test the theory, I created 2 folders with subfolders with following structure:
To visually demonstrate the structure of my folders, I am using tree program (recursive directory listening program). In Linux Mint, you can easily install it via apt install procedure.
sudo apt install tree
I am currently in ~/Documents folder, which acts as my working folder. You can check in which folder you are in with pwd command. Let me add SecondFolder to the directory stack and then the FirstFolder.
pushd ~/Documetns/Folder2/SecondFolder pushd ~/Documents/Folder1/FirstFolder
Check the status of the directory stack with dirs -v command.
You can see both of the folders added and as well first working directory (~/Documents).
I can easily traverse to the folder with specific index number. Let’s say we want to move to the folder Folder2/SecondFolder. pushd +1 (that means you want to go to the index location 1 in the list) is the command with index number (pushd +#).
We were moved to the requested folder. If we check directory stack, it has changed. FirstFolder was added to the stack on the last index (remember, LIFO 🐱👤).
Efficient way to remove it from the stack
I don’t want to have ~/Documents on the list. I can use popd +1. If I do that, it will remove item from the stack and move me to Documents folder).
Let’s assume, I don’t want to change folder. I just want to remove the item from the list to keep it organized.
popd -n +1
I am staying in current directory and ~/Documents was removed from the list. Exactly how I wanted it to perform.
popd and pushd are useful built-in commands to jump around your machine. I usually work on one project and then I remember, that I have some reusable code in another folder. By the time I wanted to copy or move some code around, I forget last editing location and need to traverse my commands history to get back.
With this commands (pushd/popd), I can easily jump around and create list of my current working directories and quickly switch between them. cd here, cd there, popd and I am back to my starting point.
In the beginning the indexing part can be a little bit annoying due to not staying the same all the time, but when you get used to it (LIFO approach), you can be quick and efficient. If you combine that with drop-down terminal, your productivity can increase significantly.
I will for sure add it to my daily commands usage going forward with Linux.