Operations 10 min read

Master Linux Directory Navigation with pushd, popd, and dirs

Learn how to efficiently switch between directories in Linux using the built‑in bash commands pushd, popd, and dirs, covering basic usage, stack manipulation, and advanced options to eliminate repetitive typing and streamline your workflow.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux Directory Navigation with pushd, popd, and dirs

When working in a Linux shell, repeatedly typing long paths with cd can be tedious. Besides the command‑line history, the built‑in bash functions pushd, popd and dirs provide a directory stack that makes navigation faster and less error‑prone.

Using command‑line history

If the two directories you switch between are close, you can simply press the up‑arrow to recall the previous cd command, reducing keystrokes.

Why use pushd and popd ?

When jumping between unrelated paths, a directory stack is more convenient. pushd adds the current directory and the target directory to a stack, allowing you to move back and forth without re‑typing full paths.

Basic cd - shortcut

The cd - command returns to the previous directory by using the $OLDPWD variable.

[root@localhost ~]# cd /usr/share/kde4/apps/kget/pics/
[root@localhost pics]# cd -
/root
[root@localhost ~]# cd -
/usr/share/kde4/apps/kget/pics

Viewing the stack with dirs

dirs

lists the directories stored in the stack. Options -p and -v format the output, showing one directory per line and adding numeric indices.

Switching between two recent directories

Use pushd without arguments to swap the top two entries of the stack.

[root@localhost kget]# pushd /boot/grub/
/boot/grub /usr/share/kde4/apps/kget /usr/local/sbin ~
[root@localhost grub]# dirs -v
0  /boot/grub
1  /usr/share/kde4/apps/kget
2  /usr/local/sbin
3  ~
[root@localhost grub]# pushd
/usr/share/kde4/apps/kget /boot/grub /usr/local/sbin ~

Switching among multiple directories

Use pushd +n where n is the stack index (starting at 0) to jump directly to that entry and rotate it to the top.

[root@localhost grub]# dirs -v
0  /boot/grub
1  /usr/share/kde4/apps/kget
2  /usr/local/sbin
3  ~
[root@localhost grub]# pushd +2
/usr/local/sbin ~ /boot/grub /usr/share/kde4/apps/kget

Removing entries with popd

popd

removes the top directory from the stack and switches to the new top. With an argument +n, it removes the nth entry.

[root@localhost sbin]# dirs -v
0  /usr/local/sbin
1  ~
2  /boot/grub
3  /usr/share/kde4/apps/kget
[root@localhost sbin]# popd
~ /boot/grub /usr/share/kde4/apps/kget
[root@localhost ~]# popd +1
~ /usr/share/kde4/apps/kget

Advanced usage

Manipulating the stack without changing directories: pushd -n /path adds /path to the stack but keeps the current directory.

Clearing the stack: dirs -c removes all entries except the current one.

[root@localhost ~]# pushd -n /boot/grub
~ /boot/grub /usr/share/kde4/apps/kget
[root@localhost ~]# dirs -v
0  ~
1  /boot/grub
2  /usr/share/kde4/apps/kget
[root@localhost ~]# dirs -c
~

Note that the directory at the top of the stack is always the current working directory and cannot be popped.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxBashpopdpushdDirectory Stack
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.