The Inefficiency of the cd Command
If you are a Linux system administrator or a web developer, you likely spend your day rapidly jumping between completely unrelated directories. For example, you might be editing a configuration file deep in /etc/nginx/sites-available/, and then you need to jump to /var/log/nginx/ to check the error logs, and then jump to /var/www/html/public/ to verify the website files.
The standard way to navigate is using the cd (Change Directory) command. However, if you are deep in the /etc/ directory and jump to /var/log/, your previous location is instantly erased from the terminal’s memory. If you want to go back to edit the configuration file, you must type the entire absolute path of /etc/nginx/sites-available/ all over again.
To solve this, Linux includes two incredibly powerful navigation commands: pushd (Push Directory) and popd (Pop Directory). These commands allow you to save your current location to a temporary memory stack before you jump, allowing you to instantly teleport back with a single keystroke.
Step 1: Pushing a Directory to the Stack
The pushd command operates exactly like the cd command—it changes your current directory. However, right before it moves you, it saves your current location to a hidden list (the “stack”).
Assume you are currently sitting in /etc/nginx/sites-available/. You need to check the logs.
Instead of typing cd /var/log/nginx/, type:
pushd /var/log/nginx/
Two things happen instantly:
- Your terminal will output text showing the stack:
/var/log/nginx /etc/nginx/sites-available - Your directory will change to
/var/log/nginx/.
Step 2: Popping Back to Your Original Location
Now that you are in the log directory, you read the error message and realize you made a typo in the config file. You need to go back.
Because you used pushd, the terminal remembers exactly where you were. You do not need to type a path. Simply run:
popd
This command “pops” the top directory off the memory stack and instantly teleports you back to /etc/nginx/sites-available/. The terminal outputs the new, shortened stack to confirm you are back where you started.
Step 3: Stacking Multiple Directories
The term “stack” is literal. You can use pushd multiple times in a row, stacking 5, 10, or 20 different directories on top of each other like a pile of plates.
If you run:
pushd /var/www/
pushd /home/user/downloads/
pushd /tmp/
You have now buried your original location under three layers of directories. Every time you type popd, the terminal will walk you backward through time, sequentially popping you back through /downloads/, then back to /var/www/, and finally back to your original starting point.
Step 4: Viewing the Current Stack
If you have been frantically jumping around the server using pushd for hours, you might forget what directories are currently saved in memory.
You can view the entire contents of the stack without actually moving by using the dirs command:
dirs -v
The -v (verbose) flag outputs a highly readable, numbered list of every directory currently saved in memory. The directory at index 0 is always your current physical location, while 1, 2, and 3 are the saved locations waiting for you to popd back to them.