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

Best OSINT Tools for Journalists 2026: Verify Sources, Images and Claims

Journalists use OSINT to verify public information before publishing. In 2026, misinformation, AI-generated images, fake…

9 hours ago

Install Docker on Ubuntu 20.04: Complete Step-by-Step Guide

Docker is an open-source platform that lets you package and run applications inside containers. Each container…

19 hours ago

Install PostgreSQL on Ubuntu: Database Setup and Admin Guide

PostgreSQL (often called Postgres) is an open-source relational database system. It supports advanced features like JSON…

20 hours ago

Install Xrdp Remote Desktop on Ubuntu: Setup and Connect

Xrdp is an open-source server that lets you connect to your Ubuntu machine from another computer…

20 hours ago

Tomcat 9 on Ubuntu 20.04: Install, Configure, and Start

Apache Tomcat is an open-source web server and Java servlet container. It is one of the…

20 hours ago

Automatic Updates on Ubuntu: Set Up unattended-upgrades

Keeping your Ubuntu system updated is one of the best ways to protect it. Security…

22 hours ago