4 Useful Command Line Techniques Worth Learning First
In this article
The four command line techniques that pay off fastest are tab completion, pipes, command history search, and chaining commands with &&. Together they cut typing dramatically, let small tools work as one, and stop you retyping commands you already ran.
Time 10 min · Steps 4 · Level Easy
The Four Techniques, In the Order to Learn Them
- Tab completion. Type the first letters of a file, folder or command name and press Tab. If there is only one match, the shell finishes it for you; if there are several, press Tab twice to see the list. It works mid-command too: type
cd Docthen Tab andDocumentscompletes. - Pipes. The
|character sends the output of one command into the input of another. The classic isls | grep report, which shows only filenames containing "report". Pipes are how the command line stays small: each tool does one job and you snap them together. - History search. Press Ctrl+R (on most Linux and macOS shells, and in Windows Terminal with PowerShell or the Command Prompt), then type part of an old command. The shell recalls the most recent match; press Ctrl+R again to step back further. Press Enter to run it, or the left arrow key to edit it first.
- Chaining with &&. Put
&&between two commands and the second runs only if the first succeeds:cd project && npm test. If the folder does not exist, the test never starts, so you avoid confusing errors from the wrong directory.
Which Technique Fits Which Job
Pick the technique by what is slowing you down, not by what looks clever. Typing long paths is a tab completion problem; too much output is a pipe problem; a command you ran an hour ago is a history problem; two steps that must happen in order is a chaining problem.
| Symptom | Technique | Example |
|---|---|---|
| Long file or folder names | Tab completion | cd Doc<Tab> |
| Too much output to read | Pipe to filter | ls | grep report |
| Retyping a recent command | Ctrl+R search | Ctrl+R, then "backup" |
| Step 2 must not run if step 1 fails | Chain with && | mkdir d && cd d |
A rule of thumb worth memorising: if you have typed more than about ten characters of a filename, you should have pressed Tab. If you have retyped a whole command from memory, you should have searched your history instead.
Pipes in Practice: One Command, Three Tools
Pipes shine when the output is bigger than your screen. Suppose you want the five largest items in a folder. On Linux or macOS, ls -S | head -5 sorts by size and shows only the top five. Count matches instead of scrolling: ls | grep log | wc -l prints how many filenames contain "log". Each tool is simple; the pipe is what turns them into an answer.
Two neighbours of the pipe are worth knowing on the same day. The redirect > writes output to a file: ls > list.txt. The double redirect >> appends instead of overwriting: ls >> list.txt adds to the end of the file. On Windows Command Prompt the same symbols work the same way, which makes these among the most portable techniques in the whole article.
History Search and the Semicolon Alternative
History search has a quieter companion: the plain history command (Linux, macOS and Windows PowerShell all have some form of it) lists your recent commands with numbers. On many shells you can rerun one by typing ! followed by its number, though the exact behaviour differs between bash, zsh and PowerShell, so treat that as a bonus rather than a core habit. Ctrl+R is the habit to build, because it needs no number and no memory of what you typed.
Chaining also has a sibling: the semicolon. cd project; npm test runs the second command no matter what happened to the first. That is useful when the steps are independent, and a trap when they are not. Choose && when failure should stop the chain, and ; only when each step stands on its own.
What to Check When a Technique Does Nothing
Tab completion doing nothing usually means there is no unique match yet, so press Tab a second time to see the options. On Windows, note that Tab in the Command Prompt cycles through matches instead of listing them, and menu labels and key behaviour can differ slightly by system and shell version. Ctrl+R doing nothing may mean the shell in use is not bash or zsh; PowerShell has its own history cmdlets, and in Windows Terminal some keys are remapped in Settings, so check there first. A pipe returning nothing usually means the filter matched nothing, so loosen the search word before assuming the pipe failed. If && seems to skip the second command, the first one is failing silently, so run it on its own to see the error.
Where to Practise Without Risk
Practise on a scratch folder, not your real files. Make one with mkdir practice, move into it with cd practice, and create a few empty files with touch a.txt b.txt c.txt. Then try each technique: Tab-complete the folder name, pipe ls through grep, chain the mkdir and cd with &&, and recall your earlier commands with Ctrl+R. Ten minutes in a throwaway folder builds the muscle memory, and nothing there matters if a command behaves unexpectedly. One caution worth stating plainly: never pipe or redirect into a command you have not read, and be slow with anything that deletes, since the command line has no undo.
Official source: GNU Bash Manual