Linux shell commands cheat sheet
Every example was run by our test suite with bash in a fresh folder (shown as ~/demo) containing fruits.txt (the lines apple, banana, cherry, apple) and docs/note.md (the line hello). Commands chained with ; or && run one after another. For the basics, start with terminal basics and pipes and redirection.
Setup commands run before each example
mkdir -p docs
printf "apple\nbanana\ncherry\napple\n" > fruits.txt
printf "hello\n" > docs/note.mdNavigatingFiles and foldersViewing and countingSearching and text toolsRedirection, variables and scripts
Navigating
| Syntax | What it does | Example → result |
|---|---|---|
pwd | Print the current directory | pwd→ ~/demo |
cd dir | Change directory | cd docs && pwd→ ~/demo/docs |
ls dir | List a directory | ls docs→ note.md |
basename path | Last part of a path | basename docs/note.md→ note.md |
dirname path | Path without its last part | dirname docs/note.md→ docs |
Files and folders
| Syntax | What it does | Example → result |
|---|---|---|
mkdir -p a/b | Create nested folders | mkdir -p a/b/c && ls a/b→ c |
touch file | Create an empty file | touch new.txt && ls new.txt→ new.txt |
cp src dst | Copy a file | cp fruits.txt copy.txt && ls copy.txt→ copy.txt |
mv old new | Move or rename | mv docs/note.md docs/readme.md && ls docs→ readme.md |
rm file | Delete a file (no trash) | rm fruits.txt; ls | wc -l→ 1 |
find dir -name pattern | Find files by name | find . -name "*.md"→ ./docs/note.md |
Viewing and counting
| Syntax | What it does | Example → result |
|---|---|---|
cat file | Print a file | cat docs/note.md→ hello |
head -n N file | First N lines | head -n 1 fruits.txt→ apple |
tail -n N file | Last N lines | tail -n 2 fruits.txt | head -n 1→ cherry |
wc -l | Count lines | wc -l < fruits.txt→ 4 |
Searching and text tools
| Syntax | What it does | Example → result |
|---|---|---|
grep -c word file | Count matching lines | grep -c apple fruits.txt→ 2 |
grep -n word file | Matching lines with line numbers | grep -n cherry fruits.txt→ 3:cherry |
sort -u | Sort and drop duplicates | sort -u fruits.txt | wc -l→ 3 |
sort | uniq -c | Count each distinct line | sort fruits.txt | uniq -c | head -n 1→ 2 apple |
sed "s/a/b/" | Replace text in each line | sed "s/apple/pear/" fruits.txt | head -n 1→ pear |
tr a-z A-Z | Translate characters | echo hello | tr a-z A-Z→ HELLO |
cut -d, -fN | Pick a field from delimited text | echo a,b,c | cut -d, -f2→ b |
awk | Small text programs; NR = line number | awk "END { print NR }" fruits.txt→ 4 |
xargs cmd | Turn input into arguments | echo docs | xargs ls→ note.md |
Redirection, variables and scripts
| Syntax | What it does | Example → result |
|---|---|---|
cmd > file | Write output to a file (replace) | echo hi > out.txt && cat out.txt→ hi |
cmd >> file | Append output to a file | echo a > out.txt; echo b >> out.txt; wc -l < out.txt→ 2 |
NAME=value | Set a shell variable | NAME=Ada; echo "Hi $NAME"→ Hi Ada |
$(( )) | Integer arithmetic | echo "Total: $((2 + 3))"→ Total: 5 |
$? | Exit status of the last command (0 = success) | false; echo $?→ 1 |
a || b | Run b only if a fails | ls nope 2>/dev/null || echo missing→ missing |
test -f file | Check that a file exists | test -f fruits.txt && echo yes→ yes |
chmod +x file | Make a script executable | printf "echo ran\n" > run.sh; chmod +x run.sh; ./run.sh→ ran |