LAB-NAV-04 - Directory Stacks & Bookmarks
Directory Stacks & Bookmarks
Master pushd and popd for mental teletransportation, allowing you to juggle multiple deep directory paths without getting lost.
- 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.
-
pushd(Push Directory): You are standing in/var/log. You usepushd /etc.- Linux takes a piece of paper, writes
/var/logon it, and pushes it down onto the spike. - Then, it changes your directory to
/etc.
- Linux takes a piece of paper, writes
-
dirs(Show Directories): You look at the spike to see what bookmarks you have saved. -
popd(Pop Directory): You are done in/etc. You typepopd.- Linux pops the top piece of paper off the spike (
/var/log). - It instantly teleports you to that directory.
- Linux pops the top piece of paper off the spike (
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.
$ 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.
$ 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.
$ 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
pushdwithout arguments — If you typepushdwith 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.- 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.
- Empty stack errors — If you type
popdand 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
- Open your terminal. Ensure you are in your home directory (
cd ~). - We need to go check
/tmp, but save our place. Type:pushd /tmp - Notice the output:
/tmp ~(You are in tmp, home is on the stack). - Verify your location:
pwd. You are in/tmp.
Exercise G2: Stacking multiple locations
- Now we need to go to
/var/log. Type:pushd /var/log - Run:
dirs -vto see your stack vertically. - You should see
0 /var/log,1 /tmp,2 ~.
Exercise G3: Popping back
- We are done with logs. Time to go back to
/tmp. - Type:
popd. - Verify your location:
pwd. You are magically back in/tmp. - Type:
dirs -v. Notice/var/logis gone from the stack. - Pop again to go home:
popd. - 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:
- Start in your home directory.
- Bookmark your place and go to
/etc. - Bookmark your place and go to
/var. - Bookmark your place and go to
/tmp. - Check your stack vertically. You should be in
/tmp, with/var,/etc, and home on the stack. - Pop three times to teleport back through
/var, then/etc, and finally landing home.
Exercise S2: The Path Expansion Trick
- Empty your stack (close terminal and reopen, or just
popduntil it errors). - Start in
/etc. pushd /var/log- Type
cd ~1and hit Enter. - 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.
- Navigate to
/tmp. - Use
pushdto go to/var/log. - Find a small file in
/var/log(e.g.,bootstrap.logoralternatives.log). - Without moving, copy that file to your
/tmpdirectory using the stack variable shortcut (~1).- Example command structure:
cp target_file ~1/
- Example command structure:
popdto return to/tmpand uselsto verify the file was copied successfully.
Mastering pushd/popd separates the 5-year veterans from the 10-year wizards.