Parse an Nginx access log and print the top 10 client IPs by request count.
BeginnerHands-onBash scripting tasks
Answer
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -10
Key trick → uniq -c only collapses adjacent lines, so you must sort before it, then sort -rn to rank by the count.
More on Bash scripting tasks
Write the safe header you start every Bash script with, and explain each part.BeginnerWrite a script that alerts when any filesystem is above 80% usage.BeginnerWrite a backup script that tars a directory with a timestamp and keeps only the last 7 backups.IntermediateWrite a health-check loop that polls a URL until it returns 200, with a timeout.IntermediateParse command-line flags `-e <env>`, `-v` (verbose) and `-h` in a Bash script.IntermediateUse `trap` to clean up a temp directory on exit, even on Ctrl+C or error.Intermediate