Overview
When working in Linux, typing long paths with the cd command repeatedly to move between deep directory levels is inefficient.
By using pushd and popd, you can remember visited directories as a “stack.” This allows you to easily return to previous locations or use shortcuts to move between saved directories. These are standard features built into shells like Bash.
Specifications (Arguments and Options)
Syntax
pushd [options] [directory]popd [options]dirs [options]
Main Options: pushd (Add and Move)
| Option | Description |
| Directory Name | Moves to the specified directory and adds it to the top of the stack. |
| +N | Moves the Nth entry from the left (starting at 0) to the top (current directory). |
| -N | Moves the Nth entry from the right (starting at 0) to the top (current directory). |
| -n | Adds the directory to the stack without changing the current directory. |
Main Options: popd (Remove and Return)
| Option | Description |
| (No arguments) | Removes the top entry (current) from the stack and moves to the next directory. |
| +N | Removes the Nth entry from the left (starting at 0) without moving. |
| -N | Removes the Nth entry from the right (starting at 0) without moving. |
| -n | Removes the directory from the stack without changing the current directory. |
Main Options: dirs (List Stack)
| Option | Description |
| -v | Displays stack contents line-by-line with index numbers (0, 1, 2…). |
| -c | Clears all entries from the directory stack. |
| -p | Displays stack contents line-by-line without numbers. |
| -l | Shows absolute paths instead of using the tilde (~) for the home directory. |
Basic Usage
This is a typical workflow: move to a directory to work, then return to the original location.
# Check current location
pwd
# -> /home/developer
# Move to /etc/nginx and remember the current location
pushd /etc/nginx
# After finishing work, return to the original location (/home/developer)
popd
Example Output:
# When running pushd (shows the stack: current_dir original_dir)
/etc/nginx /home/developer
# When running popd (removed from stack, returns to original)
/home/developer
Practical Commands
Rotating Through Multiple Working Directories
If you are moving between three different directories, you can stack them and use pushd +N to switch instantly.
# 1. Move to log directory (add to stack)
pushd /var/log/nginx
# 2. Move to config directory (add to stack)
pushd /etc/nginx/conf.d
# 3. Move to project directory (add to stack)
pushd /home/developer/project_v2
# Check stack status with numbers
dirs -v
# Jump back to index "2" (/var/log/nginx)
pushd +2
Example Output (dirs -v):
0 /home/developer/project_v2
1 /etc/nginx/conf.d
2 /var/log/nginx
3 /home/developer
Note: After running pushd +2, the list rotates, making /var/log/nginx the current (index 0).
Temporary Directory Changes in Shell Scripts
Use this to move to a directory within a script and ensure you return to the start point, while hiding unnecessary output.
# Move only if successful, then perform tasks
pushd /var/www/html > /dev/null && {
echo "Current: $(pwd)"
# File operations here
ls -l index.html
# Return to original location silently
popd > /dev/null
}
Customization Tips
Suppressing Output (> /dev/null)
By default, pushd and popd display the stack status after moving. If you do not want to see this, discard the output.
pushd /tmp > /dev/null
Setting Aliases
For faster typing, add aliases to your .bashrc file.
alias d='dirs -v'
alias po='popd'
Important Notes
- Index Shifts (Rotation):
pushd +Nrotates the stack. The target directory moves to index 0, causing other indices to change. It is best to check index numbers withdirs -vfrequently. - Session Specific: Stack information is only valid within the current shell session. It is lost when you close the terminal window and is not shared with other windows.
- Built-in Commands: These are shell built-ins, not external files in
/bin/. You cannot run them assudo pushd ....
Advanced Usage
Clearing the Directory Stack
If the history becomes too cluttered, you can reset it.
# Clear the stack, keeping only the current directory
dirs -c
Adding to History Without Moving
Use this if you are in one place but want to save a path for later use.
# Add /opt/backup to the stack without moving there
pushd -n /opt/backup
Summary
pushd and popd act like “back buttons” or “bookmarks” for your terminal.
They significantly reduce the need for the cd command, especially when managing servers or developing code across multiple directories. Start by using pushd to move and popd to return once your task is complete.
