Logs are mostly repetition. The same IP shows up hundreds of times, and sort is the fastest way to make that visible.
The core pattern:
sort | uniq -c | sort -nr
Applied to failed SSH logins:
grep "Failed password" /var/log/auth.log | awk '{print $(NF-3)}' | sort | uniq -c | sort -nr | head -10
Output looks like 412 203.0.113.45: the failure count, then the source IP. Check the field position on a sample line, since log formats vary. On RHEL-family systems, use /var/log/secure.
Three gotchas worth knowing:
-
uniqonly collapses adjacent duplicates, so the firstsortis required. -
sort -k2runs to the end of the line. Use-k2,2to sort on one field. -
sort file > fileempties the file. Usesort -o file fileinstead.
A high count shows what stands out, not what is malicious. Correlate it with successful logins, then harden SSH with keys, no root login, and MFA.
I wrote a fuller guide with 20+ examples covering -n, -h, -V, CSV sorting, and matching logs against a threat-intel list with comm:
https://www.xpert4cyber.com/2026/09/linux-sort-command-guide.html
What's the first command you run on a suspicious log?
Top comments (0)