Master Linux Directory Navigation with pushd, popd, and dirs
When you need to jump between deep directories on Linux, the traditional cd command quickly becomes cumbersome, so this guide explains how the directory‑stack commands pushd, popd, and dirs work, their options, and provides concrete examples to streamline your workflow.
On Linux the cd command is the most basic way to change directories, but repeatedly typing long paths quickly becomes inefficient. The shell provides a directory‑stack mechanism—managed by pushd, popd and dirs —that lets you navigate, reorder, and inspect a stack of directories.
Understanding the directory stack
The stack stores directory paths in a last‑in‑first‑out (LIFO) order, with the top element always representing the current working directory. Manipulating the stack automatically updates the current directory.
Viewing the stack: dirs
The dirs command lists the stack contents. Useful options are: -p: print each entry on a separate line. -v: print each entry with its index in the stack. -c: clear the entire stack.
Example:
[alvin@VM_0_16_centos dir2]$ pwd
/home/alvin/test/dir2
[alvin@VM_0_16_centos dir2]$ dirs -v
0 ~/test/dir2
1 ~/test/dir1
2 ~/test/dir3
3 ~/testThe first entry (index 0) always matches the current directory.
Adding and switching directories: pushd
1. pushd <dir> – changes to <dir> and pushes the previous directory onto the stack.
[alvin@VM_0_16_centos test]$ pushd dir1
~/test/dir1 ~/test2. pushd (no arguments) – swaps the top two entries, effectively changing to the directory that was previously second on the stack.
[alvin@VM_0_16_centos dir3]$ pushd
~/test/dir2 ~/test/dir3 ~/test/dir1 ~/test3. pushd +/-n – jumps directly to the entry at index n. A leading + counts from the top down, while - counts from the bottom up.
[alvin@VM_0_16_centos dir2]$ pushd +2
~/test/dir1 ~/test ~/test/dir2 ~/test/dir3Removing directories: popd
1. popd (no arguments) – removes the top entry; the new top becomes the current directory.
[alvin@VM_0_16_centos dir3]$ popd
~/test/dir1 ~/test/dir22. popd +/-n – deletes the entry at index n without changing the current directory.
[alvin@VM_0_16_centos dir1]$ popd +1
~/test/dir1 ~/test/dir2By combining pushd to populate the stack and pushd +/-n or popd +/-n to jump or prune entries, you can switch among many deep directories with just a few keystrokes, avoiding the repetitive use of cd.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
