← Files & Text
Linux · Files & Text
How to sort, deduplicate, and count lines in Linux
Sort lines, remove or count duplicates, and count lines, words, and bytes with sort, uniq, and wc.
Order and summarize text. uniq only collapses adjacent duplicates, so sort first. The sort | uniq -c | sort -rn pipeline gives a ranked frequency count.
# Sort (numeric, reverse)
sort -rn <file>
# Unique lines (sort first)
sort <file> | uniq
# Count how many times each line appears, ranked
sort <file> | uniq -c | sort -rn
# Count lines, words, bytes
wc <file>
wc -l <file>Linuxsortuniq