Linux

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.

Cyber Defence

Recent Posts

git fetch vs git pull: How They Work and When to Use Each

Both git fetch and git pull talk to a remote repository, but they do very different things to your…

5 days ago

git cherry-pick Command: Apply Commits from Another Branch

Sometimes the change you need already exists, just on the wrong branch. A hotfix lands…

5 days ago

Best Email APIs for Secure Business Email: Why Developers Are Moving Beyond SMTP

Email is still one of the most important communication channels inside modern applications. Password resets,…

6 days ago

Nginx Commands in Linux: Start, Stop, Reload, Test, and Log

Nginx is a high-performance web server and reverse proxy trusted by some of the largest…

1 week ago

ufw Command in Linux: Manage Firewall Rules with Examples

ufw (Uncomplicated Firewall) sits on top of iptables (or nftables on newer systems) and replaces…

1 week ago

who Command in Linux: Show All Logged-In Users and Sessions

When you share a server with a team or investigate unexpected activity, the first question…

1 week ago