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

sort and uniq

sort — Sort lines

sort file.txt              # Alphabetical sort
sort -r file.txt           # Reverse sort
sort -n numbers.txt        # Numeric sort
sort -k2 file.txt          # Sort by 2nd column
sort -u file.txt           # Sort and remove duplicates
sort -t: -k3 -n /etc/passwd  # Sort passwd by UID

uniq — Remove duplicates

⚠️ uniq only removes adjacent duplicates. Always sort first!

sort file.txt | uniq          # Remove duplicates
sort file.txt | uniq -c       # Count occurrences
sort file.txt | uniq -d       # Show only duplicates
sort file.txt | uniq -u       # Show only unique lines

Practical examples

# Most common commands in history
history | awk '{print $2}' | sort | uniq -c | sort -rn | head -10

# Count unique IP addresses in a log
cat access.log | awk '{print $1}' | sort | uniq -c | sort -rn

# Find duplicate lines
sort file.txt | uniq -d

Practice tasks

  1. Sort the contents of /etc/passwd alphabetically
  2. Create a file with duplicate lines, then remove duplicates with sort and uniq
  3. Sort /etc/passwd numerically by the 3rd field (UID)
  4. Count how many times each line occurs after sorting

Commands in this lesson

Text Processing