Intermediate · 25 min · practise every command in the built-in terminal

🛠 Project: Daily Log Report

Every morning your boss asks the same three questions about the website: How many visits did we get? How many pages were missing? Who visited the most? Instead of digging through the log each day, you'll write a script that answers them.

Yesterday's log is in ~/logs/access.log — one line per request:

203.0.113.42 - - [24/Oct/2026:09:14:07 +0200] "GET /shop HTTP/1.1" 200 2048 "-" "Mozilla/5.0 …"

Split by spaces, field $1 is the visitor's address and field $9 is the status (200 found, 404 not found).

Find the answers first

wc -l < ~/logs/access.log                          # How many lines = how many requests
awk '$9 == 404' ~/logs/access.log | wc -l          # Only the lines whose 9th field is 404
awk '{print $1}' ~/logs/access.log | sort | uniq -c | sort -rn | head -3   # Requests per address, most first

Then the script

The report should look like this:

Requests: 120
Not found (404): 14
Top visitor: 203.0.113.42
echo "Requests: $(wc -l < ~/logs/access.log)"       # $(…) puts a command's answer into the text
mkdir -p ~/reports
~/scripts/report.sh > ~/reports/report.txt         # Save what the script prints

💡 head -1 | awk '{print $2}' keeps only the address from a line like 31 203.0.113.42.

Practice tasks

  1. How many requests are in ~/logs/access.log? Print the number
  2. Which address sent the most requests? Find out with a pipeline
  3. How many requests ended in 404 (not found)? Print the number
  4. Write ~/scripts/report.sh, a bash script that prints the three lines of the report, and make it executable
  5. Run it and save today's report as ~/reports/report.txt

Commands in this lesson

Projects