Fundamentals 7 min read

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.

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

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

dirs

simply 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  ~/test

The 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 ~/test

Running pushd without arguments swaps the top two entries:

[alvin@VM_0_16_centos dir3]$ pushd
~/test/dir2 ~/test/dir3 ~/test/dir1 ~/test

Using an index to jump:

[alvin@VM_0_16_centos dir2]$ pushd +2
~/test/dir1 ~/test ~/test/dir2 ~/test/dir3

Removing 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/dir2

Example of removing a specific entry:

[alvin@VM_0_16_centos dir1]$ popd +1
~/test/dir1 ~/test/dir2

Clearing 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.

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.

command-linepopdpushdDirectory Stackdirs
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

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.