Practice Use drills for recall and labs for real operating judgment.

LAB-NAV-04 - Directory Stacks & Bookmarks

Master pushd and popd for mental teletransportation, allowing you to juggle multiple deep directory paths without getting lost.

NAV Navigation Mastery

Directory Stacks & Bookmarks

Master pushd and popd for mental teletransportation, allowing you to juggle multiple deep directory paths without getting lost.

30 min INTERMEDIATE LINUX Curriculum-reviewed
Success criteria
  • Master pushd and popd for mental teletransportation, allowing you to juggle multiple deep directory paths without getting lost.
  • Repeat the workflow without copy-paste or step-by-step prompting.

Part A: The Field Guide


🎯 What & Why

You know cd - acts like an “Undo” button, bouncing you between your current folder and the last folder you were in.

But what if you are working across three or four different deep directories? Imagine configuring a web server:

  • You need to be in /var/www/html/ to edit the code.
  • You need to be in /etc/nginx/sites-available/ to edit the server config.
  • You need to be in /var/log/nginx/ to check the error logs.

Typing these absolute paths over and over is infuriating.

The Directory Stack (pushd, popd, and dirs) acts like a physical stack of bookmarks. It lets you “save” your current location, jump somewhere else, and then instantly teleport back to your saved bookmark when you’re done.


🧠 Mental Model

Think of a physical spike on a restaurant order ticket counter.

  1. pushd (Push Directory): You are standing in /var/log. You use pushd /etc.

    • Linux takes a piece of paper, writes /var/log on it, and pushes it down onto the spike.
    • Then, it changes your directory to /etc.
  2. dirs (Show Directories): You look at the spike to see what bookmarks you have saved.

  3. popd (Pop Directory): You are done in /etc. You type popd.

    • Linux pops the top piece of paper off the spike (/var/log).
    • It instantly teleports you to that directory.

You can push multiple bookmarks onto the stack. When you pop them, they come off in reverse order (Last In, First Out).


📖 Command Reference

pushd — Bookmark and Jump

Saves your current directory to the stack, and immediately cds to the new destination.

Pushing a bookmark

$ pwd /home/alice/projects/webapp/src/components

$ # I need to check a log file, but I don’t want to lose my place here $ pushd /var/log/nginx /var/log/nginx ~/projects/webapp/src/components

$ pwd /var/log/nginx

Notice that pushd automatically runs dirs to show you the current state of the stack. The directory on the far left is your current location. The ones on the right are your saved bookmarks.

dirs — View the Stack

Shows your current bookmarks.

Viewing the stack

$ dirs -v 0 /var/log/nginx 1 ~/projects/webapp/src/components 2 /etc/ssh

The -v (vertical) flag is the most useful, as it numbers the stack. Index 0 is always your current directory.

popd — Teleport Back

Removes the top bookmark from the stack and jumps you there.

Popping a bookmark

$ popd ~/projects/webapp/src/components

$ pwd /home/alice/projects/webapp/src/components

💡 Pro Trick: Jumping without Popping

If you have 5 directories in your stack and you want to jump to bookmark #2 without deleting the bookmarks above it, you can run pushd +2. This rotates the stack, bringing bookmark #2 to the top and teleporting you there.


🌍 Real Scenarios

Scenario 1: The Config/Log cycle

$ cd /etc/nginx/sites-available    # Editing my web config
$ pushd /var/log/nginx             # Jump to logs to see why it broke
$ tail -f error.log                # Ah, syntax error
$ popd                             # Bang, instantly back editing config

Scenario 2: The deep dive extraction

$ cd /home/bob/Downloads           # I have a zip file here
$ pushd /opt/custom/software/bin   # Go deep into the system
$ # (Realize I need the zip file path)
$ cp ~2/mysoftware.zip .           # Shell expands ~2 to the 2nd directory in stack!

⚠️ Gotchas & Pitfalls

  1. pushd without arguments — If you type pushd with no path, it swaps the top two directories in the stack. This is rarely what beginners want and causes confusion. Always provide a path: pushd /destination.
  2. Closing the terminal wipes the stack — The directory stack is held in memory for your current session only. If you close your terminal window, your bookmarks are gone.
  3. Empty stack errors — If you type popd and the stack is empty (you haven’t pushed anything), bash will yell at you: popd: directory stack empty.

Part B: The Drill Deck

Terminal Required: Open your Linux terminal for these exercises.


G
Guided Step by step - type exactly this and compare the result
>

Exercise G1: Your first push

  1. Open your terminal. Ensure you are in your home directory (cd ~).
  2. We need to go check /tmp, but save our place. Type: pushd /tmp
  3. Notice the output: /tmp ~ (You are in tmp, home is on the stack).
  4. Verify your location: pwd. You are in /tmp.

Exercise G2: Stacking multiple locations

  1. Now we need to go to /var/log. Type: pushd /var/log
  2. Run: dirs -v to see your stack vertically.
  3. You should see 0 /var/log, 1 /tmp, 2 ~.

Exercise G3: Popping back

  1. We are done with logs. Time to go back to /tmp.
  2. Type: popd.
  3. Verify your location: pwd. You are magically back in /tmp.
  4. Type: dirs -v. Notice /var/log is gone from the stack.
  5. Pop again to go home: popd.
  6. Verify your location: pwd. You are home.
S
Solo Task described, hints available - figure it out
>

Exercise S1: The Sysadmin Triangle

Using ONLY pushd and popd (do not use cd), perform this workflow:

  1. Start in your home directory.
  2. Bookmark your place and go to /etc.
  3. Bookmark your place and go to /var.
  4. Bookmark your place and go to /tmp.
  5. Check your stack vertically. You should be in /tmp, with /var, /etc, and home on the stack.
  6. Pop three times to teleport back through /var, then /etc, and finally landing home.

Exercise S2: The Path Expansion Trick

  1. Empty your stack (close terminal and reopen, or just popd until it errors).
  2. Start in /etc.
  3. pushd /var/log
  4. Type cd ~1 and hit Enter.
  5. Run pwd. Where are you? The shell uses ~1, ~2, etc., as aliases for the directories in your stack!
M
Mission Real scenario - no hints, combine multiple skills
>

Mission M1: The Three-Point Hop

Your mission is to perform a complex file operation utilizing the stack as variables.

  1. Navigate to /tmp.
  2. Use pushd to go to /var/log.
  3. Find a small file in /var/log (e.g., bootstrap.log or alternatives.log).
  4. Without moving, copy that file to your /tmp directory using the stack variable shortcut (~1).
    • Example command structure: cp target_file ~1/
  5. popd to return to /tmp and use ls to verify the file was copied successfully.

Mastering pushd/popd separates the 5-year veterans from the 10-year wizards.