Bash Cheat Sheet
Wrong Windows copy
sed -i 's/\r$//' file
Test setup
Every example here was run on the 8-line access.log below, and the output shown is the real output. Tested with GNU sed 4.9, GNU grep 3.11 and mawk 1.3.4 on Linux.
192.168.1.10 - alice [27/Oct/2026:10:00:01 +0100] "GET /index.html HTTP/1.1" 200 1024
192.168.1.11 - bob [27/Oct/2026:10:00:05 +0100] "POST /login HTTP/1.1" 401 312
10.0.0.5 - - [27/Oct/2026:10:01:10 +0100] "GET /admin HTTP/1.1" 403 210
192.168.1.10 - alice [27/Oct/2026:10:02:00 +0100] "GET /img/a.png HTTP/1.1" 200 5120
10.0.0.5 - - [27/Oct/2026:10:02:30 +0100] "POST /login HTTP/1.1" 401 312
10.0.0.5 - - [27/Oct/2026:10:02:35 +0100] "POST /login HTTP/1.1" 401 312
172.16.4.2 - carol [27/Oct/2026:10:03:00 +0100] "GET /api/v1/items HTTP/1.1" 500 87
192.168.1.11 - bob [27/Oct/2026:10:04:00 +0100] "POST /login HTTP/1.1" 200 640
Field numbers used below (space-delimited): $1 client IP, $3 user, $9 HTTP status, $NF response bytes (last field).
Command reference
Eleven commands cover most log work. Flags marked (GNU) are not guaranteed on BSD or macOS.
| Command | Does | Flags worth knowing |
|---|---|---|
| head | first lines of input | -n N first N lines; -n -N all but the last N (GNU) |
| tail | last lines of input | -n N; -n +N from line N onward; -f follow a growing file (not run here, it is interactive) |
| cat | concatenate and print | -n number the lines |
| cut | pick columns | -d delimiter; -f fields; -c characters |
| sort | order lines | -n numeric; -r reverse; -u unique; -k key; -t separator |
| uniq | collapse adjacent duplicates, so sort first | -c count; -d only repeated lines; -u only single lines |
| wc | count | -l lines; -w words; -c bytes |
| grep | filter by pattern | -E extended regex; -v invert; -i ignore case; -c count; -o only the match; -n line numbers; -A/-B/-C context lines; -F literal text; -P Perl regex (GNU) |
| sed | stream editor | -E extended regex; -n no auto-print; s/old/new/g substitute; /re/d delete; -i.bak edit in place with backup |
| awk | field-based mini language | -F separator; $N field N; $NF last field; NR line number; c[$1]++ counts per key |
| split | cut a file into pieces | -l lines per piece; -b bytes per piece; -d numeric suffixes; –additional-suffix=.log (GNU) |
One tested example per command, with what it printed on the sample log:
head -n 3 access.log # first 3 lines
head -n -6 access.log # all but the last 6 -> 2 lines
tail -n 2 access.log # last 2 lines
tail -n +8 access.log # from line 8 on -> 1 line
cat -n access.log # numbered lines
cut -d' ' -f1,9 access.log # IP and status, e.g. 192.168.1.10 200
cut -c1-12 access.log # first 12 characters, e.g. 192.168.1.10
cut -d' ' -f1 access.log | sort -t. -k1,1n -k2,2n -k3,3n -k4,4n -u
# unique IPs in numeric order (4 lines)
cut -d' ' -f1 access.log | sort | uniq -d # IPs seen more than once (3 of 4)
cut -d' ' -f1 access.log | sort | uniq -u # IPs seen exactly once -> 172.16.4.2
wc -l < access.log # 8 (no filename printed)
wc -w < access.log # 80
wc -c < access.log # 631
grep -c '" 401 ' access.log # 3
grep -v ' 200 ' access.log | wc -l # 5 lines that are not status 200
grep -i 'GET /ADMIN' access.log # 1 line, case-insensitive
grep -n '/login' access.log # prefixes 2: 5: 6: 8:
grep -A1 ' 403 ' access.log # the match plus the next line
grep -oE '([0-9]{1,3}\.){3}[0-9]{1,3}' access.log | sort -u # the 4 IPs only
sed -n '2,3p' access.log # lines 2 to 3 only
sed '/login/d' access.log | wc -l # 4 lines remain
sed -E 's/\[[^]]*\]/[TS]/' access.log # [27/Oct/2026:...] becomes [TS]
sed -i.bak 's/alice/USER/' t.log # edits t.log, keeps t.log.bak
awk '$9>=400 {print $1, $9}' access.log # 5 lines, e.g. 10.0.0.5 403
awk 'END{print NR}' access.log # 8
split -l 3 -d --additional-suffix=.log access.log part_
# part_00.log (3), part_01.log (3), part_02.log (2)
split -b 300 -d access.log chunk_ # 3 files; a byte split can cut a line in half
Chaining patterns
Most log counting is one idiom: extract a field, sort, count with uniq -c, then sort the counts. All outputs below are from the sample log.
# Top talkers: requests per client IP
cut -d' ' -f1 access.log | sort | uniq -c | sort -rn
# 3 10.0.0.5
# 2 192.168.1.11
# 2 192.168.1.10
# 1 172.16.4.2
# Status code histogram (equal counts may print in either order)
awk '{print $9}' access.log | sort | uniq -c | sort -rn
# 3 401
# 3 200
# 1 500
# 1 403
# Failed logins per IP, worst first, top 5
grep 'POST /login' access.log | grep '" 401 ' | awk '{print $1}' | sort | uniq -c | sort -rn | head -5
# 2 10.0.0.5
# 1 192.168.1.11
# How many requests were 401 or 403?
grep -E '" (401|403) ' access.log | wc -l
# 4
# How many client or server errors (status 400 and up)?
awk '$9>=400' access.log | wc -l
# 5
# How many distinct client IPs?
cut -d' ' -f1 access.log | sort -u | wc -l
# 4
# Single busiest IP only
awk '{print $1}' access.log | sort | uniq -c | sort -rn | head -n 1
# 3 10.0.0.5
# Total bytes served
awk '{s+=$NF} END{print s}' access.log
# 8017
# Bytes per status code (awk array, then sort the keys)
awk '{b[$9]+=$NF} END{for(k in b) print k, b[k]}' access.log | sort
# 200 6784
# 401 936
# 403 210
# 500 87
awk can replace several stages on its own: awk '{c[$1]++} END{for(k in c) print c[k], k}' does the counting of sort | uniq -c in one process, and its output still needs a final sort -rn because awk arrays have no defined order.
Anonymising a log
Choose the method by what you still need afterwards: a full mask for sharing, a partial mask for subnet statistics, a hash when per-IP counts must survive. Each example shows the first line of output.
# Mask every IPv4 address completely
sed -E 's/\b([0-9]{1,3}\.){3}[0-9]{1,3}\b/x.x.x.x/g' access.log
# x.x.x.x - alice [27/Oct/2026:10:00:01 +0100] "GET /index.html HTTP/1.1" 200 1024
# Keep the first two octets, mask the rest (subnet-level statistics still work)
sed -E 's/\b([0-9]{1,3}\.[0-9]{1,3})\.[0-9]{1,3}\.[0-9]{1,3}\b/\1.x.x/g' access.log
# 192.168.x.x - alice [27/Oct/2026:10:00:01 +0100] "GET /index.html HTTP/1.1" 200 1024
# Replace usernames in field 3 (keep "-" for anonymous requests)
awk '{ if ($3!="-") $3="user"; print }' access.log
# 192.168.1.10 - user [27/Oct/2026:10:00:01 +0100] "GET /index.html HTTP/1.1" 200 1024
# Replace each IP with a short hash: same IP -> same token, so counting still works
awk '{ cmd="printf %s \"" $1 "\" | sha256sum | cut -c1-8"; cmd | getline h; close(cmd); $1=h; print }' access.log
# 805ebf20 - alice [27/Oct/2026:10:00:01 +0100] "GET /index.html HTTP/1.1" 200 1024
# Write to a new file; do not edit the original in place
sed -E 's/\b([0-9]{1,3}\.[0-9]{1,3})\.[0-9]{1,3}\.[0-9]{1,3}\b/\1.x.x/g' access.log > access.anon.log
Caveats:
- Count before you mask. After the full mask every client is x.x.x.x, so a per-IP count collapses into one bucket.
- A bare hash of an IPv4 address can be reversed by trying all 2^32 addresses. For data you will share, hash with a secret key or salt (for example an HMAC); this was not run here.
- The hashing one-liner starts two processes per line, so it is slow on large logs. It was tested on the 8-line sample only.
- Assigning to an awk field rebuilds the line with single spaces, which changes any irregular spacing in the original.
- The IPv4 patterns do not match IPv6 addresses.
\bis a GNU sed extension. - IPs are not the only identifiers: URLs, query strings, user agents and timestamps can still point at a person. Review the output before sharing it.
One pipeline: filter, anonymise, count, split
This single pipeline keeps only 401 and 403 requests, masks the last two IP octets, replaces named users, saves the anonymised lines with tee, counts per masked subnet, and then splits the saved file. Run on the sample log it produced the output shown.
grep -E '" (401|403) ' access.log \
| sed -E 's/\b([0-9]{1,3}\.[0-9]{1,3})\.[0-9]{1,3}\.[0-9]{1,3}\b/\1.x.x/' \
| awk '$3!="-"{$3="user"} {print}' \
| tee failed.anon.log \
| awk '{print $1}' | sort | uniq -c | sort -rn
# 3 10.0.x.x
# 1 192.168.x.x
split -l 2 -d --additional-suffix=.log failed.anon.log failed_part_
# failed_part_00.log (2 lines), failed_part_01.log (2 lines)
Stage by stage: grep filters, sed masks, awk replaces users, tee saves a copy and passes the stream on, the last four commands count. failed.anon.log holds lines such as 10.0.x.x - - [27/Oct/2026:10:01:10 +0100] "GET /admin HTTP/1.1" 403 210.
Gotchas
| Symptom | Cause and fix |
|---|---|
| uniq -c shows many 1s for the same value | uniq only merges adjacent lines. On the sample, cut -d' ' -f1 access.log piped straight into uniq -c gave 7 groups instead of 4; add sort first. |
| Numbers sort as 10, 100, 9 | Default sort is lexical. Use sort -n (gives 9, 10, 100). |
| wc -l prints a filename | Pass the file by redirection instead: wc -l < access.log prints just 8. |
| sed ignores a + in the pattern | Basic regex treats + literally. echo aab with sed 's/a+/X/' printed aab; with sed -E it printed Xb. |
| cut -f gives empty fields | cut treats every single space as a delimiter, so runs of spaces create empty fields. awk splits on runs of whitespace, so prefer awk for aligned columns. |
| Script test of a match, no output wanted | grep -q prints nothing and returns exit code 0 on a match, 1 on none (1 confirmed here). |
| Mixed-case sort order differs between machines | Sorting follows the locale. Prefix with LC_ALL=C for plain byte order. I did not test other locales here, because only the C locale was installed. |
| Pipeline works on Linux, fails on macOS | BSD tools lack some GNU options used here: head -n -N, grep -P, sed \b, split –additional-suffix. |
| awk results differ between systems | The tests ran on mawk 1.3.4. gawk is the common alternative and adds extras; plain field, array and END code behaves the same. I did not run gawk here. |
| Big file, many pieces | split -l counts lines and keeps records whole; split -b counts bytes and can cut a line in half. |