Essential Command-Line Skills
The handful of commands and shortcuts that make you fast and dangerous in any terminal.
The command line feels intimidating until you realize most real work uses a tiny core of commands, glued together. Master these categories and you can operate any Unix-like system:
- Look around:
ls,pwd,cat,less,head,tail - Find things:
find,grep,which - Move data:
cp,mv,rm,mkdir - Inspect the system:
ps,top,df,free - Combine commands: the pipe
|and redirection>
The real superpower is composition: small tools that each do one thing, piped
together to answer a question. cat access.log | grep 404 | wc -l counts your
404 errors — three simple tools forming an answer.
Say a service is misbehaving and you want to see what’s eating memory and find recent errors:
# Top 5 memory-hungry processes
ps aux --sort=-%mem | head -n 6
# How much disk is free?
df -h
# Find ERROR lines from today's app log and show the last 20
grep -i error /var/log/myapp.log | tail -n 20
# Count how many times each IP hit the server (classic one-liner)
awk '{print $1}' access.log | sort | uniq -c | sort -rn | headNotice the pattern: generate a stream of text, filter it, then summarize. That shape solves a huge fraction of day-to-day ops problems.
- Use
history | grep cdto find everycdcommand you’ve run this session. - Create a file with three lines, then show only the middle line using a
combination of
headandtail. - Use
grep -rn "TODO" .to find every TODO comment in a project folder, with file names and line numbers. - Pipe
ls -lintowc -lto count how many items are in a directory.
Cheat Sheet▾
| Task | Command |
|---|---|
| Search file contents | grep -rni "pattern" . |
| Find files by name | find . -name "*.log" |
| First / last N lines | head -n 20 / tail -n 20 |
| Follow a log live | tail -f app.log |
| Count lines | wc -l |
| Sort & dedupe | sort | uniq -c | sort -rn |
| Redirect output to file | cmd > out.txt (overwrite) >> (append) |
| Pipe into next command | cmd1 | cmd2 |
| Re-run last command | !! |
| Run last command as root | sudo !! |
| Search history | Ctrl + R |
| Cancel a running command | Ctrl + C |
Common Interview Questions▾
What does a pipe `|` do?
It connects the standard output of one command to the standard input of the next, letting you chain small tools into a processing pipeline without temporary files.
Difference between `>` and `>>`?
> redirects output to a file, overwriting it. >> appends to the file,
preserving existing contents.
How do you watch a log file update in real time?
tail -f /path/to/file — it prints new lines as they’re written. Add -n 100
to also show the last 100 existing lines.
Troubleshooting Scenarios▾
A command “hangs” with no output.
It may be waiting on standard input. Press Ctrl+C to cancel. Some tools (like
cat with no arguments) read from the keyboard until you press Ctrl+D.
grep returns nothing but you expect matches.
Check case sensitivity (grep -i), and remember grep treats the pattern as a
regex — special characters like . or * may need escaping or -F for a fixed
string.
“command not found”.
The program isn’t installed or isn’t on your PATH. Check with which <cmd> and
install it via your package manager.