Chapter 02
Files and text tools
Linux tools treat text as lines. Small programs that each do one thing well can be chained to answer most questions about logs and data.
Viewing files
Also called cat, less, head, tail -f
- What
catprints a file,lesspages through it (search with/, quit withq),headandtailshow the first or last lines, andtail -ffollows a file as it grows.- Why
- Reading configs and following logs live is most of what you do on a server.
less /var/log/syslog
head -n 20 data.csv
tail -n 100 app.log
tail -f /var/log/nginx/access.logWatch out
cat on a huge log file floods the terminal; use less or tail instead.
find
Also called locate, fd
- What
findwalks a directory tree and matches files by name, type, size, age or owner, then prints them or runs an action on each.- Why
- It answers "where is that config?", "what filled the disk?" and "which files changed today?".
find . -name '*.log'
find /var -type f -size +100M
find . -type f -mtime -1
find /tmp -type f -mtime +7 -delete
find . -name '*.js' -not -path './node_modules/*'Watch out
Leaving the pattern unquoted, so the shell expands *.log in the current directory before find ever sees it.
grep
Also called ripgrep, rg, search text
- What
grepprints lines matching a pattern. Useful flags:-iignore case,-rrecurse,-nline numbers,-vinvert,-ccount,-Eextended regex,-oonly the match,-A/-B/-Clines of context.- Why
- It is the fastest way to find an error in a log or a setting in a codebase.
grep -rn 'DATABASE_URL' --include='*.ts' src/
grep -i 'error' app.log | grep -v 'healthcheck'
grep -c ' 500 ' access.log
grep -E 'timeout|refused' -C 2 app.logsed
Also called stream editor, find and replace
- What
sededits text as it streams past, most often substituting withs/old/new/g.-iedits files in place;-nwithpprints only selected lines.- Why
- Quick replacements across files and config tweaks in scripts, without opening an editor.
sed 's/http:/https:/g' links.txt
sed -i 's/^PORT=.*/PORT=8080/' .env
sed -n '10,20p' big.log
sed '/^#/d' config.iniWatch out
sed -i differs on macOS, which needs sed -i ''. Scripts that must run on both break on this.
awk
Also called columns, fields
- What
awksplits each line into fields ($1,$2,$NFfor the last) and runs a small program per line, with patterns, variables and sums.- Why
- It pulls columns out of logs and command output and totals them in one line.
awk '{print $1}' access.log
awk -F, '{print $2, $5}' data.csv
awk '$9 >= 500 {print $7}' access.log
awk '{sum += $10} END {print sum / NR}' access.logsort, uniq, cut and wc
Also called counting, top N
- What
sortorders lines (-nnumeric,-rreverse,-kby column),uniqcollapses adjacent duplicates (-ccounts them),cutextracts fields by delimiter, andwc -lcounts lines.- Why
- Chained together they answer "top 10 IPs", "how many distinct users" and similar questions instantly.
cut -d' ' -f1 access.log | sort | uniq -c \
| sort -rn | head -10
cut -d: -f1 /etc/passwd | sort
wc -l *.csvWatch out
uniq without sort first: it only removes duplicates that sit next to each other.
xargs
Also called batch arguments
- What
xargsreads items from standard input and passes them as arguments to a command, in batches.-0pairs withfind -print0to handle names with spaces;-Pruns in parallel.- Why
- Many commands take arguments, not input; xargs connects a list from one command to another that needs arguments.
find . -name '*.tmp' -print0 | xargs -0 rm -f
grep -rl 'oldName' src/ | xargs sed -i 's/oldName/newName/g'
cat urls.txt | xargs -n 1 -P 4 curl -sOWatch out
Piping plain find output into xargs: filenames with spaces split into several arguments. Use -print0 and -0.
diff and comparing files
Also called diff -u, cmp, sha256sum
- What
diff -ushows line differences between two files in unified format.sha256sumprints a checksum to confirm two files, or a download, are identical.- Why
- Comparing a working config with a broken one, or checking a download is intact, is a quick way to find what changed.
diff -u nginx.conf.bak nginx.conf
sha256sum release.tar.gz