Part 1 · Basics · Lesson 04 · 45 minutes
Read and search text in logs
- Create files, and read them in full, in part and as they grow
- Search the lines of a file or a folder with grep
- Count, sort and cut text to rank events
The lab folder as it stands after this lesson. Unzip it in ~/krlab.
More
The zip holds every file that the course has printed up to here. To start this lesson with the files of the earlier lessons only, take the zip of the lesson before. Type the code yourself first: you learn more.
Scenario
Your team lead forwards an alert: a login service saw many failures in one morning. The log has only ten lines in your lab, but on a real server it has 50,000, and nobody reads that by eye. "Tell me which address made the failures, and which accounts it tried," she says. Then she gives you a second file, the log of a web server, and asks the same kind of question.
Recall
What does echo "hi $name" print, and what does echo 'hi $name' print?
With double quotes the shell replaces $name by its value, for example hi Ada. With single quotes it prints hi $name as typed.
What does "ls -S" show first?
The biggest file. -S sorts by size, and -r turns the order round.
The idea
Most of what a security analyst reads is plain text: logs, configuration files, lists. Linux has one small tool for each job on text, and each tool does one thing well. You read with cat, less, head and tail, you search with grep, and you count and arrange with wc, sort, uniq and cut. The tool for tests and sums, awk, has its own lesson (lesson 5).
| Tool | Job |
|---|---|
touch | Make an empty file, or set the time of a file to now. |
cat | Print a whole file. |
less | Show a file one screen at a time. |
head, tail | Print the first, or the last, lines. |
wc | Count lines, words or bytes. |
grep | Print the lines that match a pattern. |
sort, uniq | Put lines in order, and merge equal neighbours. |
cut | Keep some columns of each line. |
The pipe | joins these tools: it sends the output of the command on its left into the command on its right. Lesson 6 explains pipes in full. Here you meet one long pipe at the end:
- auth.log → grep: every line goes in
- grep → grep -o: only the failed lines stay
- grep -o → cut: only "from ADDRESS" stays
- cut → sort: only the address stays
- sort → uniq -c: equal lines are now neighbours
- uniq -c → sort -rn: each group gets a count
Worked example
1. Make an empty file with touch
touch makes an empty file when the name is new. When the file exists, touch changes only its time of last change. ls -l shows the size in bytes.
mkdir -p ~/krlab/linux-basics && cd ~/krlab/linux-basics
touch empty.txt
ls -l empty.txt-rw-rw-r-- 1 you you 0 Nov 21 10:51 empty.txt2. Build a fake login log
cat (concatenate) prints files. Here it reads from a here-document: <<'EOF' tells the shell to give cat every line up to the line EOF, and > writes them into the file auth.log. The addresses come from ranges that are reserved for examples.
cat > auth.log <<'EOF'
Nov 21 08:01:12 krbox sshd[1021]: Failed password for root from 203.0.113.50 port 40122 ssh2
Nov 21 08:01:19 krbox sshd[1023]: Failed password for invalid user admin from 203.0.113.50 port 40130 ssh2
Nov 21 08:02:03 krbox sshd[1030]: Accepted publickey for alice from 192.0.2.10 port 51234 ssh2
Nov 21 08:07:55 krbox sshd[1140]: Failed password for bob from 198.51.100.7 port 60001 ssh2
Nov 21 08:08:02 krbox sshd[1140]: Accepted password for bob from 198.51.100.7 port 60001 ssh2
Nov 21 08:09:30 krbox sshd[1155]: Failed password for invalid user test from 203.0.113.50 port 40188 ssh2
Nov 21 08:09:34 krbox sshd[1155]: Failed password for invalid user oracle from 203.0.113.50 port 40190 ssh2
Nov 21 08:15:10 krbox sudo[1201]: bob : user NOT in sudoers ; TTY=pts/1 ; PWD=/home/bob ; COMMAND=/bin/cat /etc/shadow
Nov 21 08:20:44 krbox sshd[1230]: Accepted publickey for alice from 192.0.2.10 port 51300 ssh2
Nov 21 08:31:09 krbox sshd[1290]: Failed password for root from 203.0.113.50 port 40500 ssh2
EOFTake one line apart before you search it:
3. Count and read the start and the end
wc (word count) counts: -l lines, -w words, -c bytes. head and tail print the first and the last lines of a file, and -n sets how many. cat -n numbers the lines.
wc -l auth.log
wc -w auth.log
wc -c auth.log
head -n 2 auth.log
tail -n 1 auth.log10 auth.log
150 auth.log
1007 auth.log
Nov 21 08:01:12 krbox sshd[1021]: Failed password for root from 203.0.113.50 port 40122 ssh2
Nov 21 08:01:19 krbox sshd[1023]: Failed password for invalid user admin from 203.0.113.50 port 40130 ssh2
Nov 21 08:31:09 krbox sshd[1290]: Failed password for root from 203.0.113.50 port 40500 ssh2tail -f auth.log (follow) prints each new line as a program writes it, and waits for more. Press Ctrl+C to stop it.
For a big file, use less. Make a file of 1000 lines and open it. In less, Space shows the next screen, b the one before, /500 Enter searches for 500, n jumps to the next match, g goes to the start, G to the end, and q leaves.
seq 1 1000 > numbers.txt
less numbers.txt4. Search with grep
grep PATTERN FILE (global regular expression print) prints each line that contains the pattern. Five options help: -c counts the matching lines, -i ignores case, -v shows the lines that do not match, -n puts the line number in front, and -E (extended) allows | for "or".
grep -c "Failed password" auth.log
grep -i "accepted" auth.log
grep -v sshd auth.log
grep -cE "root|admin" auth.log
grep -n "bob" auth.log6
Nov 21 08:02:03 krbox sshd[1030]: Accepted publickey for alice from 192.0.2.10 port 51234 ssh2
Nov 21 08:08:02 krbox sshd[1140]: Accepted password for bob from 198.51.100.7 port 60001 ssh2
Nov 21 08:20:44 krbox sshd[1230]: Accepted publickey for alice from 192.0.2.10 port 51300 ssh2
Nov 21 08:15:10 krbox sudo[1201]: bob : user NOT in sudoers ; TTY=pts/1 ; PWD=/home/bob ; COMMAND=/bin/cat /etc/shadow
3
4:Nov 21 08:07:55 krbox sshd[1140]: Failed password for bob from 198.51.100.7 port 60001 ssh2
5:Nov 21 08:08:02 krbox sshd[1140]: Accepted password for bob from 198.51.100.7 port 60001 ssh2
8:Nov 21 08:15:10 krbox sudo[1201]: bob : user NOT in sudoers ; TTY=pts/1 ; PWD=/home/bob ; COMMAND=/bin/cat /etc/shadowTwo facts are clear: 6 logins failed, and bob tried to read /etc/shadow (the password hashes) with sudo and was refused.
To search a folder, copy the log into a sub-folder. grep -r (recursive) goes into folders but does not follow symbolic links (lesson 8), and -l lists only the names of the files that match.
mkdir -p cases/alpha && cp auth.log cases/alpha/
grep -rl "invalid user" ../auth.log
./cases/alpha/auth.logThe order can differ on your machine.
5. Cut, sort and count
cut keeps parts of each line: -c1-15 keeps characters 1 to 15 (the time), and -d' ' -f1-3,5 splits at spaces (-d, delimiter) and keeps fields 1 to 3 and 5 (-f).
cut -c1-15 auth.log | head -n 2
cut -d' ' -f1-3,5 auth.log | head -n 2Nov 21 08:01:12
Nov 21 08:01:19
Nov 21 08:01:12 sshd[1021]:
Nov 21 08:01:19 sshd[1023]:sort puts lines in order. uniq collapses lines that are equal and next to each other, so it needs sorted input. uniq -c also counts, and sort -u keeps one copy of each line. Make a list of addresses and try them.
printf '203.0.113.50\n192.0.2.10\n203.0.113.50\n198.51.100.7\n203.0.113.50\n192.0.2.10\n' > hosts.txt
sort hosts.txt | uniq -c
sort -u hosts.txt
sort hosts.txt | uniq -c | sort -rn 2 192.0.2.10
1 198.51.100.7
3 203.0.113.50
192.0.2.10
198.51.100.7
203.0.113.50
3 203.0.113.50
2 192.0.2.10
1 198.51.100.7In the last line sort -rn sorts by number (-n) with the biggest first (-r, reverse). printf prints text from a template, and \n ends a line.
6. Rank the sources of the failures
Now join the tools. grep -o prints only the part of the line that matches. -E lets a pattern use + (and |). A set in square brackets works with or without -E. In a pattern, [0-9.] means one character that is a digit or a dot, and + means one or more of the thing before it. So from [0-9.]+ matches the word from, a space and the whole address, and [a-z]+ matches one or more lower-case letters. Take the failed lines, keep the address, and count each one.
grep "Failed password" auth.log | grep -oE "from [0-9.]+" | cut -d' ' -f2 | sort | uniq -c | sort -rn 5 203.0.113.50
1 198.51.100.7One address made 5 of the 6 failures. Which user names did it try that do not exist?
grep -oE "invalid user [a-z]+" auth.log | cut -d' ' -f3 | sort -uadmin
oracle
testYour turn
Your lead gave you the log of a web server. The drawing shows one line. A status 404 means "not found", 401 "login needed", 403 "forbidden" and 200 "success".
Run this block once. It only builds the data. It is not the task.
mkdir -p ~/krlab/linux-basics/web && cd ~/krlab/linux-basics/web
cat > access.log <<'EOF'
192.0.2.21 - - [21/Nov/2026:09:00:01 +0000] "GET /index.html HTTP/1.1" 200 5120
192.0.2.21 - - [21/Nov/2026:09:00:02 +0000] "GET /style.css HTTP/1.1" 200 2048
198.51.100.9 - - [21/Nov/2026:09:01:15 +0000] "GET /index.html HTTP/1.1" 200 5120
203.0.113.77 - - [21/Nov/2026:09:02:40 +0000] "GET /admin HTTP/1.1" 403 199
203.0.113.77 - - [21/Nov/2026:09:02:41 +0000] "GET /.env HTTP/1.1" 404 199
203.0.113.77 - - [21/Nov/2026:09:02:43 +0000] "GET /backup.zip HTTP/1.1" 404 199
203.0.113.77 - - [21/Nov/2026:09:02:44 +0000] "GET /login.php HTTP/1.1" 404 199
198.51.100.9 - - [21/Nov/2026:09:04:10 +0000] "GET /about.html HTTP/1.1" 200 3300
203.0.113.77 - - [21/Nov/2026:09:05:02 +0000] "POST /login HTTP/1.1" 401 150
203.0.113.77 - - [21/Nov/2026:09:05:09 +0000] "POST /login HTTP/1.1" 401 150
192.0.2.21 - - [21/Nov/2026:09:06:30 +0000] "GET /about.html HTTP/1.1" 200 3300
203.0.113.77 - - [21/Nov/2026:09:07:12 +0000] "POST /login HTTP/1.1" 200 812
EOFWork on access.log in that folder. There are no steps here: use the tools of the worked example. Find out which client behaves like an attacker, and whether it got in. Then answer from your own terminal.
Last, make the empty file findings.txt with a tool of this lesson. Then list it with ls -l.
Check yourself
Why does "sort | uniq -c" work, but "uniq -c" alone gives a wrong count?
uniq counts only lines that are next to each other. If an address is on lines 1, 5 and 9, uniq -c counts three groups of one. sort first puts equal lines together, so uniq -c counts one group of three.
What is the difference between "grep -c error log" and "grep error log | wc -l"?
Both give the number of matching lines. grep -c does it with one program. The pipe is longer, but it works with any command in front of wc.
Bob had one failed login and then a success. Is that proof of an attack?
No. One failure and then a success from the same address is most often a typing mistake. The pattern to worry about is many failures from one address followed by a success. In the web log of your turn, the address 203.0.113.77 tried hidden paths, failed two logins and then got a 200: that is the pattern.
You run "touch report.txt" twice. What changed after the second run?
Only the time of last change. The file already existed, so touch did not make it again, and its content stayed as it was.
Field notes
- Use
grep -Ffor a fixed text. It does not read the pattern as a regular expression, so a dot or a bracket in an address cannot change the meaning of the search. - A log line holds data from outside. An attacker can send a user name that contains spaces or looks like a new log line. Do not trust free text.
- Logs rotate. Old ones are
auth.log.1andauth.log.2.gz. Search compressed files withzgrep. tail -Ffollows a log by name and carries on after a rotation.tail -fdoes not.touchalso sets any time, so a time stamp is a clue and not proof. An attacker can make a changed file look old (MITRE ATT&CK calls this "Timestomp").