Advanced
Searching inLabs
How to search
  • Search a task or a tool (lvm, selinux, @podman); filter by level and by book or course.
  • With an empty search box, a filter alone lists the labs — for example all hard labs of one course.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

Lab 21.1The Linux Command Line, Chapter 21

Part 3 practice: find, back up and analyse logs

Level
Medium
45 min
Machines
any Linux system
Status
Not started

Goal

A real admin morning: find big and old files, back up a directory with tar and rsync, and turn a log file into a small report with regular expressions, sort, uniq, sed and printf.

Before you start

  • Make test data:
    mkdir -p ~/lab3/data/{a,b,c}
    for i in {1..30}; do head -c $((i * 40000)) /dev/urandom > ~/lab3/data/a/file$i.bin; done
    touch -d '40 days ago' ~/lab3/data/a/file{1..5}.bin
    printf '%s\n' "2026-11-08 10:01 LOGIN ok user=ana ip=10.0.0.5" "2026-11-08 10:02 LOGIN fail user=root ip=203.0.113.9" "2026-11-08 10:03 LOGIN fail user=admin ip=203.0.113.9" "2026-11-08 10:04 LOGIN ok user=ben ip=10.0.0.7" "2026-11-08 10:05 LOGIN fail user=test ip=198.51.100.4" > ~/lab3/auth.log

Tasks

  1. List all files in ~/lab3/data bigger than 1 MB, with their size, biggest first.
  2. Find the files not changed for more than 30 days and compress them with gzip in one pipeline that also works with spaces in names.
  3. Make a compressed archive ~/lab3/data-YYYY-MM-DD.tar.gz (today's date in the name) of ~/lab3/data, then list its contents without extracting.
  4. Copy ~/lab3/data to ~/lab3/backup with rsync. Delete one file in the source, then sync again so the backup is the same as the source. Show what would be deleted first.
  5. From ~/lab3/auth.log: print only the failed logins, then count failures per IP address (most first).
  6. Change the date format in the log from 2026-11-08 to 08.11.2026 with sed, without changing the file.
  7. Print a table with printf: the column titles IP and FAILS, and one line per IP, aligned.

Check your work

ls ~/lab3/data/a | grep -c '\.gz$'
tar tzf ~/lab3/data-*.tar.gz | head -3
diff -r ~/lab3/data ~/lab3/backup && echo same
grep -c fail ~/lab3/auth.log
  • 5 files end in .gz.
  • diff -r prints same.
  • There are 3 failed logins; 203.0.113.9 has 2.

Hints

Show the hints

Solution

Show the solution
  1. Big files, biggest first.
    find ~/lab3/data -type f -size +1M -exec du -h '{}' + | sort -hr
  2. Compress old files safely.
    find ~/lab3/data -type f -mtime +30 -print0 | xargs -0 -r gzip
  3. Archive with the date and list it.
    tar czf ~/lab3/data-$(date +%F).tar.gz -C ~/lab3 data
    tar tzf ~/lab3/data-$(date +%F).tar.gz | head
  4. Sync, delete, dry run, sync again.
    rsync -av ~/lab3/data/ ~/lab3/backup/
    rm ~/lab3/data/a/file30.bin
    rsync -av --delete -n ~/lab3/data/ ~/lab3/backup/
    rsync -av --delete ~/lab3/data/ ~/lab3/backup/
  5. Failed logins and the count per IP.
    grep ' fail ' ~/lab3/auth.log
    grep ' fail ' ~/lab3/auth.log | grep -Eo 'ip=[0-9.]+' | cut -d= -f2 | sort | uniq -c | sort -rn
          2 203.0.113.9
          1 198.51.100.4
  6. Change the date format.
    sed 's/^\([0-9]\{4\}\)-\([0-9]\{2\}\)-\([0-9]\{2\}\)/\3.\2.\1/' ~/lab3/auth.log
  7. Print an aligned table.
    printf "%-16s %5s\n" IP FAILS
    grep ' fail ' ~/lab3/auth.log | grep -Eo 'ip=[0-9.]+' | cut -d= -f2 | sort | uniq -c | sort -rn | while read -r n ip; do printf "%-16s %5d\n" "$ip" "$n"; done

My notes

No notes yet.

Lessons for this lab

Commands in this lab