Linux Cheatsheet
Viewing and Editing Files
Use this Linux reference while you build software engineering projects, review code, or refresh the syntax you reach for most.
Viewing File Contents
cat file.txt # dump entire file cat -n file.txt # with line numbers cat -A file.txt # show non-printing chars (tabs as ^I, $ at EOL) tac file.txt # reverse (last line first)
Paging Through Files
less file.txt # page through file (preferred) more file.txt # simpler pager (forward-only)
less key bindings:
| Key | Action |
|---|---|
space / f | page forward |
b | page backward |
g / G | go to top / bottom |
/pattern | search forward |
?pattern | search backward |
n / N | next / previous match |
q | quit |
-N | toggle line numbers |
F | follow (tail -f mode) |
Head and Tail
head file.txt # first 10 lines head -n 20 file.txt # first 20 lines head -c 100 file.txt # first 100 bytes tail file.txt # last 10 lines tail -n 50 file.txt # last 50 lines tail -f /var/log/syslog # follow live (stream new lines) tail -F file.log # follow + reopen if rotated
grep — Searching Inside Files
grep "error" file.log # lines matching pattern grep -i "error" file.log # case-insensitive grep -r "TODO" ./src/ # recursive grep -n "def " app.py # show line numbers grep -c "fail" file.log # count matching lines grep -v "DEBUG" file.log # invert (exclude matches) grep -l "error" *.log # filenames only grep -w "exit" file # whole-word match grep -A 3 "error" file # 3 lines after match grep -B 3 "error" file # 3 lines before match grep -C 3 "error" file # 3 lines before and after grep -E "err|warn" file # extended regex (ERE) grep -P "\d{3}-\d{4}" file # Perl-compatible regex
Text Transformation
cut -d: -f1 /etc/passwd # field 1, delimiter ':' cut -c1-10 file.txt # characters 1–10 sort file.txt # alphabetical sort sort -n nums.txt # numeric sort sort -r file.txt # reverse sort -u file.txt # sort + unique sort -k2 -t: file # sort by field 2 uniq file.txt # remove adjacent duplicate lines sort file | uniq -c # count occurrences sort file | uniq -d # only duplicates tr 'a-z' 'A-Z' < file # transliterate tr -d '\r' < file.txt > unix.txt # strip carriage returns
sed — Stream Editor
sed 's/foo/bar/' file # replace first occurrence per line sed 's/foo/bar/g' file # replace all occurrences sed -i 's/foo/bar/g' file # in-place edit sed -i.bak 's/foo/bar/g' file # in-place with backup sed -n '5,10p' file # print lines 5–10 sed '/^#/d' file # delete comment lines sed 's/^[[:space:]]*//' file # strip leading whitespace sed -n '/start/,/end/p' file # range between patterns
awk — Pattern-Action Processing
awk '{print $1}' file # print first field (whitespace-delimited) awk -F: '{print $1}' /etc/passwd # field delimiter ':' awk '{print NR, $0}' file # prefix with line number awk '$3 > 100' file # filter: field 3 > 100 awk '{sum += $1} END {print sum}' file # sum column awk '/error/ {print $0}' file # grep-like filter awk 'NR==5' file # print line 5
Terminal Editors
nano (beginner-friendly)
nano file.txt
| Key | Action |
|---|---|
ctrl+o | save (Write Out) |
ctrl+x | exit |
ctrl+w | search |
ctrl+k | cut line |
ctrl+u | paste |
vim (powerful)
vim file.txt
| Mode | Enter with |
|---|---|
| Normal | esc |
| Insert | i (before), a (after) |
| Visual | v |
| Command | : |
| Normal command | Action |
|---|---|
dd | delete (cut) line |
yy | yank (copy) line |
p | paste after |
u | undo |
ctrl+r | redo |
/pattern | search forward |
:%s/old/new/g | global replace |
:w | save |
:q! | quit without saving |
:wq | save and quit |