Cybersecurity Updates & Tools

pushd And popd Commands In Linux: Navigate Directories Easily

Introduction

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.

What Are pushd And popd Commands?

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.

Check Current Directory

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.

Use popd To Return Back

To return to the previous directory, use:

popd

Now check your current location:

pwd

You should be back in the previous directory.

View Directory Stack

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.

Basic pushd And popd Example

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.

Cybersecurity Example: Work Inside A Scan Directory

#!/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.

Difference Between cd And pushd

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.

Conclusion

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.