Skip to content
KALANBLON
Show contents

Chapter 6 of 7 · 25 min

Project: investigating a server log

By the end of this chapter, you'll be able to

  • Explore a log file with head, wc and cut
  • Build pipes that count and rank lines
  • Write a precise grep pattern to avoid false positives
  • Bundle the analysis into a reusable script

The log

Each line follows the usual web server format: the visitor's IP address, the date and time in square brackets, the request in quotes, the HTTP status code (200 = success, 404 = page not found, 500 = server error) and the size of the response in bytes. The addresses used here are reserved for documentation: they do not belong to anyone.

printf '%s\n' \
 '203.0.113.7 - - [05/Jan/2026:09:02:11 +0000] "GET /index.html HTTP/1.1" 200 5120' \
 '198.51.100.23 - - [05/Jan/2026:09:05:40 +0000] "GET /enrol HTTP/1.1" 200 3400' \
 '203.0.113.7 - - [05/Jan/2026:09:06:02 +0000] "GET /timetable HTTP/1.1" 200 404' \
 '192.0.2.50 - - [05/Jan/2026:10:00:01 +0000] "GET /admin HTTP/1.1" 404 210' \
 '192.0.2.50 - - [05/Jan/2026:10:00:02 +0000] "GET /wp-login.php HTTP/1.1" 404 210' \
 '192.0.2.50 - - [05/Jan/2026:10:00:03 +0000] "GET /.env HTTP/1.1" 404 210' \
 '198.51.100.23 - - [05/Jan/2026:10:12:45 +0000] "POST /enrol HTTP/1.1" 500 120' \
 '203.0.113.88 - - [05/Jan/2026:10:20:10 +0000] "GET /index.html HTTP/1.1" 200 5120' \
 '192.0.2.50 - - [05/Jan/2026:10:30:00 +0000] "GET /backup.zip HTTP/1.1" 404 210' \
 '198.51.100.23 - - [05/Jan/2026:11:01:30 +0000] "POST /enrol HTTP/1.1" 500 120' \
 '203.0.113.88 - - [05/Jan/2026:11:15:00 +0000] "GET /timetable HTTP/1.1" 200 404' \
 '203.0.113.7 - - [05/Jan/2026:11:40:55 +0000] "GET /contact HTTP/1.1" 200 2048' \
 > access.log
wc -l < access.log
head -n 2 access.log
12
203.0.113.7 - - [05/Jan/2026:09:02:11 +0000] "GET /index.html HTTP/1.1" 200 5120
198.51.100.23 - - [05/Jan/2026:09:05:40 +0000] "GET /enrol HTTP/1.1" 200 3400

printf '%s\n' writes each argument followed by a line break, and a \ at the end of a line lets a long command continue on the next line. wc -l < access.log reads the file on standard input: it then displays only the number, without the file name. A real log would contain millions of lines, but the commands would be exactly the same.

Step 1: the status codes

Split on spaces, the status code is the 9th field:

cut -d ' ' -f 9 access.log | sort | uniq -c | sort -rn
      6 200
      4 404
      2 500

Two 500 errors: the server itself failed. That is the priority.

Step 2: which pages fail?

grep '" 500 ' access.log | cut -d ' ' -f 6,7
"POST /enrol
"POST /enrol

Both 500 errors come from submitting the enrolment form: that is the lead to pass on to the site's developer.

Step 3: who is using the server?

cut -d ' ' -f 1 access.log | sort | uniq -c | sort -rn | head -n 3
      4 192.0.2.50
      3 203.0.113.7
      3 198.51.100.23

The address 192.0.2.50 comes first, and its requests are odd: /admin, /wp-login.php, /.env, /backup.zip, a few seconds apart, all 404s. This is the typical behaviour of a bot looking for known vulnerabilities. Nothing was found, but it is worth reporting.

The same analysis, ready to run again tomorrow in one command.

Step 5 of 5
Text description of the animation
  1. Start by measuring: how many lines, what they look like.
  2. The breakdown of codes shows two real server errors.
  3. A precise pattern isolates the failing lines: the enrolment form is crashing.
  4. Ranking the addresses reveals a bot probing sensitive paths.
  5. The same analysis, ready to run again tomorrow in one command.

Step 4: the report script

Tomorrow, you will have to do it again. Let's bundle the analysis into a script that takes the log as an argument:

echo '#!/bin/bash' > report.sh
echo 'echo "Report for $1"' >> report.sh
echo 'echo -n "Requests: "' >> report.sh
echo 'wc -l < "$1"' >> report.sh
echo 'echo "500 errors:"' >> report.sh
echo 'grep "\" 500 " "$1" | cut -d " " -f 7 | sort | uniq -c' >> report.sh
echo 'echo "Most active addresses:"' >> report.sh
echo 'cut -d " " -f 1 "$1" | sort | uniq -c | sort -rn | head -n 2' >> report.sh
chmod u+x report.sh
./report.sh access.log
Report for access.log
Requests: 12
500 errors:
      2 /enrol
Most active addresses:
      4 192.0.2.50
      3 203.0.113.7

Two details: echo -n does not add a line break, so the number printed by wc lands right after “Requests: ”; and in the script, "$1" is in quotes so that a file name containing a space stays a single argument.

Your turn

Mini-lab: run your own investigationTerminal simulator

Terminal

Click “Run”: the commands run in a fresh terminal, in your home folder /home/awa. Type “help” for the list of commands.

Teaching simulator: it reproduces the essential Linux commands in your browser, with nothing to install · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Worked exercise

Worked exercise: the breakdown by hour

The headteacher wants to know at what times the site is busiest. From access.log, display the number of requests for each hour of the day, from earliest to latest.

Hint 1
In [05/Jan/2026:09:02:11, the hour sits between the line's first and second :.
Hint 2
cut -d : -f 2 splits on colons and keeps the 2nd piece. The lines are already in chronological order, but a sort costs nothing and makes the result reliable.
Show the solution and common mistakes
cut -d : -f 2 access.log | sort | uniq -c
      3 09
      6 10
      3 11

The 10am peak matches the bot's visit and the first enrolment errors: the two leads of the investigation meet.

Common mistakes:

  • Splitting on spaces: the 4th field contains the whole date ([05/Jan/2026:09:02:11), not just the hour.
  • Forgetting sort before uniq -c when the file is not sorted: the same hour could appear on several lines.

Summary

  1. 1An access log reads like a table: cut -d ' ' -f N extracts a column.
  2. 2The combination cut | sort | uniq -c | sort -rn ranks any column by frequency.
  3. 3A grep pattern that is too broad counts false positives: include the context around the value you look for.
  4. 4Bursts of requests to /admin or /.env give away a bot looking for vulnerabilities.
  5. 5A script with $1 makes the analysis repeatable on any log.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.