The pushd and popd commands in Linux are useful for moving between directories quickly. If you work in the terminal often, you may need to jump between multiple folders again and again. Instead of typing long cd commands repeatedly, you can use pushd and popd to manage directory navigation more efficiently.
For Bash scripting beginners, these commands are useful because they introduce the concept of a directory stack. A directory stack stores directory paths temporarily, allowing you to move back and forth between locations easily.
The pushd command changes your current directory and saves the previous directory in a stack.
The popd command returns you to a previous directory from that stack.
Basic syntax:
pushd directory_path
popd
These commands are especially useful when working with scripts, project folders, log directories, backup folders, and cybersecurity tool outputs.
Before using pushd, check your current directory:
pwd
Example output:
/home/kali
Now move to another directory using pushd:
pushd /tmp
Output may look like:
/tmp /home/kali
This means you are now in /tmp, and /home/kali is saved in the directory stack.
To return to the previous directory, use:
popd
Now check your current location:
pwd
You should be back in the previous directory.
You can view the directory stack using:
dirs
Example:
/tmp /home/kali
The first path is your current directory. The remaining paths are stored in the stack.
Create a Bash script:
nano pushd-popd-example.sh
Add this code:
#!/bin/bashecho "Current directory:"pwdecho "Moving to /tmp"pushd /tmp > /dev/nullecho "Now inside:"pwdecho "Returning back"popd > /dev/nullecho "Back to:"pwd
Run the script:
chmod +x pushd-popd-example.sh./pushd-popd-example.sh
The > /dev/null part hides the default output of pushd and popd.
#!/bin/bashscan_dir="$HOME/security-scans"mkdir -p "$scan_dir"echo "Moving to scan directory..."pushd "$scan_dir" > /dev/nullecho "Current directory: $(pwd)"echo "Creating scan report..."echo "Scan started at $(date)" > scan-report.txtpopd > /dev/nullecho "Returned to original directory: $(pwd)"
This is useful when a script needs to temporarily work inside a folder and then return to the original location.
The cd command simply changes directories. The pushd command changes directories and remembers the previous location. The popd command returns to the saved location.
Use cd for simple movement. Use pushd and popd when you need to jump between directories safely.
The pushd and popd commands in Linux make directory navigation easier. They help you move between folders while keeping track of previous locations using a directory stack.
For Bash scripting beginners, these commands are helpful for writing cleaner scripts that temporarily enter directories, perform tasks, and return safely. They are useful in automation, backups, project management, and cybersecurity scripting.
Introduction Bash scripting is a powerful way to automate Linux tasks, but writing a script…
Introduction A self-signed SSL certificate is a certificate that is created and signed by the…
Introduction Debugging is an important part of Bash scripting. When a script does not work…
Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…
Introduction Pipes are an important feature in Linux and Bash scripting. A pipe allows you…
Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…