Master Linux Directory Navigation with pushd, popd, and dirs
This guide explains how to use the Linux directory‑stack commands pushd, popd, and dirs to replace repetitive cd usage, showing their options, practical examples, and tips for efficiently switching between deep directory paths.
When navigating directories on Linux, most users reach for the basic cd command, but repeatedly typing long paths quickly becomes tedious.
Directory stack commands
The article introduces three commands that operate on the directory stack : pushd, popd, and dirs. The stack stores visited directories, with the top element always representing the current working directory.
Viewing the stack with dirs
dirssimply lists the stack contents. Common options are: -v – show each entry with its index (verbose). -p – print each entry as a full path (identical to -v except for the index display). -c – clear the entire stack.
Example output:
/home/alvin/test/dir2
[alvin@VM_0_16_centos dir2]$ dirs -v
0 ~/test/dir2
1 ~/test/dir1
2 ~/test/dir3
3 ~/testThe top entry always matches the current directory; after using pushd or popd, the current directory changes to the new top element.
Adding directories with pushd
Three typical usages:
pushd <directory> – changes to the specified directory and pushes it onto the top of the stack.
pushd (no arguments) – swaps the top two entries, effectively changing the current directory to the previous one.
pushd +/-n – jumps directly to the entry at index n (positive counts from the top, negative from the bottom).
Sample session:
[alvin@VM_0_16_centos test]$ pushd dir1
~/test/dir1 ~/test
[alvin@VM_0_16_centos dir1]$ pushd ../dir2
~/test/dir2 ~/test/dir1 ~/test
[alvin@VM_0_16_centos dir2]$ pushd ../dir3
~/test/dir3 ~/test/dir2 ~/test/dir1 ~/testRunning pushd without arguments swaps the top two entries:
[alvin@VM_0_16_centos dir3]$ pushd
~/test/dir2 ~/test/dir3 ~/test/dir1 ~/testUsing an index to jump:
[alvin@VM_0_16_centos dir2]$ pushd +2
~/test/dir1 ~/test ~/test/dir2 ~/test/dir3Removing entries with popd
Two main forms:
popd – removes the top element, causing the current directory to become the new top.
popd +/-n – deletes the element at index n (positive from the top, negative from the bottom).
Example of popping the top entry:
[alvin@VM_0_16_centos dir3]$ popd
~/test/dir1 ~/test/dir2Example of removing a specific entry:
[alvin@VM_0_16_centos dir1]$ popd +1
~/test/dir1 ~/test/dir2Clearing the stack
Use dirs -c to empty the stack entirely.
By combining pushd to populate the stack and pushd +/-n or popd to switch or remove entries, users can jump between deep directories with a single command, eliminating the need for repetitive cd invocations.
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.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
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.
