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:

KeyAction
space / fpage forward
bpage backward
g / Ggo to top / bottom
/patternsearch forward
?patternsearch backward
n / Nnext / previous match
qquit
-Ntoggle line numbers
Ffollow (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
KeyAction
ctrl+osave (Write Out)
ctrl+xexit
ctrl+wsearch
ctrl+kcut line
ctrl+upaste

vim (powerful)

vim file.txt
ModeEnter with
Normalesc
Inserti (before), a (after)
Visualv
Command:
Normal commandAction
dddelete (cut) line
yyyank (copy) line
ppaste after
uundo
ctrl+rredo
/patternsearch forward
:%s/old/new/gglobal replace
:wsave
:q!quit without saving
:wqsave and quit