7.3 cd: Changing Directories and the - Shortcut
Right, let’s talk about cd. It’s the command you’ll use more than any other, the digital equivalent of putting one foot in front of the other. The concept is laughably simple: you tell your shell where you want to go, and it takes you there. The implementation, however, has a few quirks that have tripped up everyone from absolute beginners to grizzled veterans who just weren’t paying attention. Let’s demystify it.
The Absolute Basics and the Home Directory
You type cd followed by a path. The path can be absolute, starting from the root of the entire filesystem (/), or relative, starting from your current working directory.
But here’s your first “aha” moment: what happens when you just type cd by itself, with no arguments? Go on, try it.
$ pwd
/home/you/some/deeply/nested/project
$ cd
$ pwd
/home/you
It whisked you back to your home directory! This isn’t magic; it’s just the shell’s default behavior. Using cd alone is a shortcut for cd $HOME, where $HOME is a special environment variable that always points to your user’s home. It’s the digital equivalent of shouting “I’m going home!” and teleporting there. Incredibly useful when you’re lost ten directories deep and just need to reset.
The Magical - Shortcut
Now for the real star of the show, the - shortcut. This is one of those things that feels like a secret handshake once you learn it. - (a single hyphen) as an argument to cd means “take me back to the directory I was in just before this one.”
Let’s walk through a classic scenario:
$ pwd
/home/you/projects/blog
$ cd /var/log
$ pwd
/var/log
$ cd -
/home/you/projects/blog
$ pwd
/home/you/projects/blog
See that? We jumped from our project directory to /var/log to check some server logs. The moment we were done, cd - instantly brought us back to exactly where we started. It’s a toggle between your current and previous directory. The shell is kind enough to print the path it’s taking you back to, so you get a confirmation.
Why is this so brilliant? It saves you from tedious copy-pasting of paths or trying to remember that deep directory name you just came from. It’s the shell’s version of a “Back” button. Under the hood, it’s using another special environment variable called $OLDPWD (Old Print Working Directory), which the shell automatically updates every time you change directories. cd - is literally just a convenient alias for cd $OLDPWD.
The Subtle Pitfalls and Quirks
Here’s where the designers, in their infinite wisdom, created a tiny little footgun. What do you think happens if you use cd - repeatedly?
$ pwd
/home/you/projects/blog
$ cd /var/log
$ pwd
/var/log
$ cd -
/home/you/projects/blog
$ cd -
/var/log
$ cd -
/home/you/projects/blog
It just keeps toggling you back and forth. It doesn’t traverse a history of directories, it only remembers the very last one. This is the most common misconception. It’s not a stack of where you’ve been; it’s a single slot for your last location. Don’t expect it to take you back two jumps.
Another classic pitfall: trying to cd into a file instead of a directory. You’d think the error would be obvious, but when you’re tired and autopiloting, it happens.
$ cd my_script.sh
bash: cd: my_script.sh: Not a directory
The shell yells at you, rightly so. cd is for directories only. If you need to execute that file, that’s what ./ is for (./my_script.sh).
Best Practices and Pro-Tips
Combine with
ls: One of my most frequent command sequences is jumping to a new directory and immediately seeing what’s there. You can chain these commands with&&:cd /some/new/path && ls -laThis means “change directory, and if that succeeds, then list the contents.” The “if that succeeds” part is crucial. If the
cdfails (e.g., the path doesn’t exist), thelswon’t run, preventing you from listing the contents of the wrong directory.Use Tab Completion Relentlessly: This isn’t specific to
cd, but it’s vital for it. Never type a full path if you can help it. Typecd /eand hit Tab. It’ll probably complete to/etc/. Then typepand hit Tab again. It’ll likely complete tosystemd/orpasswd. This is the fastest way to navigate and it prevents typos. If your completion doesn’t work, your shell configuration is probably broken, and we should fix that.Spaces in Paths are the Enemy: If a directory name has a space in it (whoever does this is a monster, but they exist), you can’t just
cd My Documents. The shell seesMyandDocumentsas two separate arguments. You have to escape it:cd My\ Documents # Using a backslash cd "My Documents" # Using quotesMy professional opinion? Avoid spaces in paths you expect to use in the terminal. Use underscores or dashes. Life is too short.