Three cd shortcuts everyone uses
You already know cd path and cd .. from lesson 2-1. Three more moves cover 90% of real navigation:
cdwith no argument: jump straight to your home directory (/home/youon Linux), the personal directory where your stuff lives. Its nickname is~(tilde), socd ~/projectsworks from anywhere.cd -: jump back to the directory you were in before, and print it. Great for bouncing between two places.cd /: go to the root of the tree.
Bouncing between two directories with cd -
This session visits /tmp, moves away to /, then bounces back. cd - announces the directory it is taking you to, and pwd independently confirms it.
cd /tmp cd / cd - pwd
Output
/tmp /tmp
Both output lines say /tmp, but they come from different commands. The first is printed by cd - itself, which reports its destination as a convenience. The second is the pwd that follows, verifying the shell really did land there.
Stop typing full names: Tab completion
In a real terminal, typing the first few letters of a file, directory, or command and pressing Tab makes the shell finish the name for you. When several names match, a second Tab lists all the possibilities. cd pro followed by Tab is how experienced people "type" cd projects/.
Two more keys belong in the same reflex set. ↑ / ↓ scroll through command history, and Ctrl+L clears the screen. These are not optional polish. Tab completion also prevents typos, which starts to matter a great deal once unit 3 introduces commands that delete files.
There is one more history tool professionals lean on: Ctrl+R, reverse search. It takes any fragment of an old command, searches history backwards, and shows the most recent match. Enter runs it, and pressing Ctrl+R again steps to older matches. Long commands stop being retyped and get recalled in two keystrokes instead.
A session using the shortcuts
Here is a short session that puts all three shortcuts to work. It begins in /home/sam/projects, and each block shows a command alongside the output it produced.
The first pwd establishes the starting point:
$ pwd /home/sam/projects
A bare cd, with no argument at all, jumps to the home directory. Written out in full it would be cd ~, and the two forms are equivalent:
$ cd
It printed nothing, as successful cd calls do, so a second pwd confirms the move actually happened:
$ pwd /home/sam
Finally cd - returns to the previous directory and announces where it went, which is why a path appears under the command without any pwd being needed:
$ cd - /home/sam/projects
Typing cd with no arguments from deep inside /var/log/nginx lands you in your home directory. A bare cd always goes home, equivalent to cd ~, regardless of how deep in the tree you started.
The three destinations are worth committing to muscle memory, because they cover most navigation:
| Command | Destination |
|---|---|
cd | Your home directory (~) |
cd - | The directory you were in previously |
cd / | The root of the filesystem tree |
Tab is the key that makes the shell auto-complete a half-typed name. It sits just above Caps Lock on a standard keyboard.
The shell completes the name as far as it unambiguously can. If exactly one name matches, the whole thing is filled in. If several match, the shell fills in the shared prefix and stops, and a second Tab press lists every candidate so you can add the distinguishing character. Using it constantly is both faster than typing and typo-proof, since a completed name is by definition a name that exists.