Terminal cheat sheet
Every command from the Terminal Basics series on one page, with the six rules that keep you out of trouble. A bonus post.
This is the whole Terminal Basics series on one page. Bookmark it, screenshot it, or keep it open in a second window while you practise.
What is covered: moving around, files, reading, searching, permissions, sudo, pipes and redirects. Each row says what a command does and gives an example you can copy into the practice mansion.
How this was checked: the commands and their effects come from parts 1 to 5, each run on Ubuntu 24.04 with bash 5.2 and GNU coreutils 9.4. The two help examples at the end were run for this page. The man command is not shown, because the minimal test machine ships without manual pages.
1. Start here
If you remember only one thing from this series, make it this: know where you are. The terminal is always standing in exactly one directory, and every command acts on that room unless you say otherwise. pwd tells you which room it is.
The second thing: there is no trash can. Deleting and overwriting are immediate and permanent, so practise in the mansion.
2. Finding your way around
| Command | What it does |
|---|---|
pwd | Prints the directory you are in |
ls | Lists what is in a directory |
ls -a | Includes hidden files (names starting with a dot) |
ls -F | Marks directories with a / |
ls -l | Long listing: permissions, owner, size, date |
cd dir | Moves into a directory |
cd .. | Moves up one directory |
cd | Goes straight home |
cd - | Goes back to where you just were |
Shortcuts for paths: ~ is your home directory, . is this directory, and .. is the one above. A path that starts with / or ~ works from anywhere.
3. Making, moving and deleting
| Command | What it does |
|---|---|
mkdir name | Makes a directory |
mkdir -p a/b/c | Makes a whole chain of directories |
touch file | Creates an empty file |
cp from to | Copies a file. Overwrites silently |
cp -r from to | Copies a directory and everything in it |
mv from to | Moves, or renames. Overwrites silently |
rm file | Deletes a file. No undo |
rmdir dir | Deletes an empty directory only |
rm -r dir | Deletes a directory and everything in it. No undo |
Wildcard: * matches any run of characters. ls *.txt lists every file ending in .txt. The shell expands it before the command runs.
4. Reading and searching
| Command | What it does |
|---|---|
cat file | Prints a whole file |
head -n 3 file | Prints the first 3 lines |
tail -n 3 file | Prints the last 3 lines |
less file | Reads a long file one screen at a time. q quits |
wc -l file | Counts lines |
grep word file | Prints lines containing the word |
grep -i | Ignores capital letters |
grep -n | Shows line numbers |
grep -c | Counts matching lines |
grep -v | Prints lines that do not match |
grep -r word . | Searches every file below here |
find . -name "x" | Finds files by name |
find . -type d | Finds directories only |
Phrases with spaces need quotes: grep "stove door" complaints.log. And remember: grep searches inside files, find searches for the files themselves.
5. Permissions and sudo
| Command | What it does |
|---|---|
whoami | Prints your username |
groups | Lists your groups |
ls -l | Shows owner, group and permissions |
chmod +x file | Makes a file runnable |
./file | Runs a file in this directory |
chmod 755 file | rwxr-xr-x: owner all, others read and run |
chmod 644 file | rw-r--r--: owner read and write, others read |
chmod 600 file | rw-------: private to you |
sudo command | Runs one command as root. A skeleton key |
Reading -rw-r--r--: the first character is the type (- file, d directory), then three trios for the owner, the group and everyone else. Read is 4, write is 2 and execute is 1, so add them up: rwx is 7, rw- is 6, r-x is 5, r-- is 4.
6. Pipes and redirects
| Command | What it does |
|---|---|
a | b | Feeds the output of a into b |
> file | Saves output to a file. Replaces it |
>> file | Adds output to the end of a file |
2> file | Saves error messages to a file |
2> /dev/null | Throws error messages away |
sort | Puts lines in order |
uniq -c | Counts adjacent duplicates. Sort first |
sort -rn | Biggest number first |
cut -d' ' -f3 | Takes the third space-separated column |
The classic recipe for "what is most common?" is sort | uniq -c | sort -rn.
7. Questions and their commands
| You want to know | Use |
|---|---|
| Where am I? | pwd |
| What is in here, including hidden things? | ls -a |
| Which of these are directories? | ls -F |
Where is the file called x? | find . -name "x" |
| Which file mentions this word? | grep -r word . |
| How many lines match? | grep -c word file |
| How long is this file? | wc -l file |
| What is the most common line? | sort file | uniq -c | sort -rn |
| Why will this script not run? | ls -l script.sh, then chmod +x script.sh and ./script.sh |
| Who am I, and what am I allowed to do? | whoami, ls -l |
8. The six rules of the haunted terminal
- Know where you are. Run
pwdwhen in doubt. - There is no trash can.
rm,>,cpandmvdo not ask. Practise in the mansion. - Silence usually means success. A command that works often prints nothing.
- Read the error. It tells you what is wrong: a spelling, a missing file, a lock.
>replaces,>>adds. One arrow too few and the file is gone.sudois a skeleton key. Understand the command first.Permission deniedis a lock, not a bug.
9. Getting help
Most commands will tell you how to use them. Add --help. It prints a lot, so here we show only the first three lines, using a pipe:
ls --help | head -n 3Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.Some commands, like cd, are built into the shell itself and do not have --help. For those, ask the shell:
help cd | head -n 2cd: cd [-L|[-P [-e]] [-@]] [dir]
Change the shell working directory.Many systems also have a manual: man ls opens the full manual page for ls, and q leaves it. Our minimal test machine does not ship manual pages, so we cannot show it here, but it is worth trying on your own computer.
Quick check
Say the command before you read the answer.
- You want to count how many lines in
complaints.logmentionWARN. - You want to find a file called
love-letters.txtsomewhere under the current directory. - You want to make
vault.shrunnable and then run it. - You want to save the output of
lsinto a file without destroying the file if it already exists.
Answers
grep -c WARN complaints.logfind . -name "love-letters.txt"chmod +x vault.sh, then./vault.shls >> listing.txt. One arrow would replace the file. Two add to it.
Where next
You now have the commands that cover most of everyday terminal work: finding things, making and breaking things safely, reading logs, locking and unlocking, and chaining small tools into big answers. The mansion is yours. Keep practising in it, and rebuild it whenever you like with bash ~/mansion-setup.sh.
More Linux tutorials are on the way.