Unit 3 · Linux^2 · Lesson 26 of 34
Sort of Unique?
Getting your life (and your files) in order.
sort
sort does exactly what it sounds like: it sorts the lines of a file in alphabetical order.
That last one matters more than you'd think. Without -n, Linux sorts numbers like they're words, so 10 ends up before 9 because "1" (the first character in 10) comes before "9".
You can also stack flags, so sort -rn sorts by number, biggest first.
uniq
uniq gets rid of duplicate lines, buuuut here's the catch… it only removes duplicates that are right next to each other. That's why you'll almost always see it paired with sort first.
[user@linux]$ sort names.txt | uniq
Counting with uniq -c
Add the -c flag and uniq will also tell you how many times each line showed up. This is great for stuff like figuring out which IP address shows up the most in a log file.
[user@linux]$ sort ips.txt | uniq -c
Then sort -n that one more time, and the biggest count floats all the way to the bottom.
🕵️ Cyber tip: In cybersecurity, the IP address that shows up the most is usually the one you should be suspicious of.
Try it
There's a folder called sus in your home directory:
scores.txtis the club's game night leaderboard, totally out of orderips.txtis every IP address that hit our server today, one per line, all jumbled up
One of those IPs showed up WAY more than it should have. Find out who it is.
Live Linux terminal
This lesson comes with a real Ubuntu machine that runs in your browser, so you type every command yourself. Create a free account to do this part.
Do this lesson for real
With a free account you get the terminal, the challenges and the checks that go with every lesson in Intro to Linux, and your progress is saved as you go. CyberAcad is a 501(c)(3) nonprofit, so there is nothing to pay.