TetSharing

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.md

NavigatingFiles and foldersViewing and countingSearching and text toolsRedirection, variables and scripts

SyntaxWhat it doesExample → result
pwdPrint the current directorypwd
→ ~/demo
cd dirChange directorycd docs && pwd
→ ~/demo/docs
ls dirList a directoryls docs
→ note.md
basename pathLast part of a pathbasename docs/note.md
→ note.md
dirname pathPath without its last partdirname docs/note.md
→ docs

Files and folders

SyntaxWhat it doesExample → result
mkdir -p a/bCreate nested foldersmkdir -p a/b/c && ls a/b
→ c
touch fileCreate an empty filetouch new.txt && ls new.txt
→ new.txt
cp src dstCopy a filecp fruits.txt copy.txt && ls copy.txt
→ copy.txt
mv old newMove or renamemv docs/note.md docs/readme.md && ls docs
→ readme.md
rm fileDelete a file (no trash)rm fruits.txt; ls | wc -l
→ 1
find dir -name patternFind files by namefind . -name "*.md"
→ ./docs/note.md

Viewing and counting

SyntaxWhat it doesExample → result
cat filePrint a filecat docs/note.md
→ hello
head -n N fileFirst N lineshead -n 1 fruits.txt
→ apple
tail -n N fileLast N linestail -n 2 fruits.txt | head -n 1
→ cherry
wc -lCount lineswc -l < fruits.txt
→ 4
SyntaxWhat it doesExample → result
grep -c word fileCount matching linesgrep -c apple fruits.txt
→ 2
grep -n word fileMatching lines with line numbersgrep -n cherry fruits.txt
→ 3:cherry
sort -uSort and drop duplicatessort -u fruits.txt | wc -l
→ 3
sort | uniq -cCount each distinct linesort fruits.txt | uniq -c | head -n 1
→ 2 apple
sed "s/a/b/"Replace text in each linesed "s/apple/pear/" fruits.txt | head -n 1
→ pear
tr a-z A-ZTranslate charactersecho hello | tr a-z A-Z
→ HELLO
cut -d, -fNPick a field from delimited textecho a,b,c | cut -d, -f2
→ b
awkSmall text programs; NR = line numberawk "END { print NR }" fruits.txt
→ 4
xargs cmdTurn input into argumentsecho docs | xargs ls
→ note.md

Redirection, variables and scripts

SyntaxWhat it doesExample → result
cmd > fileWrite output to a file (replace)echo hi > out.txt && cat out.txt
→ hi
cmd >> fileAppend output to a fileecho a > out.txt; echo b >> out.txt; wc -l < out.txt
→ 2
NAME=valueSet a shell variableNAME=Ada; echo "Hi $NAME"
→ Hi Ada
$(( ))Integer arithmeticecho "Total: $((2 + 3))"
→ Total: 5
$?Exit status of the last command (0 = success)false; echo $?
→ 1
a || bRun b only if a failsls nope 2>/dev/null || echo missing
→ missing
test -f fileCheck that a file existstest -f fruits.txt && echo yes
→ yes
chmod +x fileMake a script executableprintf "echo ran\n" > run.sh; chmod +x run.sh; ./run.sh
→ ran

Official docs

Compiled by Ethan Cole (pen name) with AI-assisted drafting; every example in the table is executed by our test suite. Related lessons: terminal basics, files, pipes redirection.