How to Sort and Deduplicate Text with sort and uniq
The sort command orders lines of text and uniq removes consecutive duplicates. sort names.txt prints the file alphabetically, and sort names.txt | uniq -c counts how often each line appears. Together they are the standard way to summarize lists.
Sort lines alphabetically
sort reads lines and prints them in order. The original file is untouched; the sorted result goes to the screen.
Sort numbers with -n
Plain sort orders text, so 10 comes before 2. Add -n for numeric order and -r to reverse (largest first). This ranks sizes or scores correctly.
Remove duplicates with uniq
uniq removes consecutive identical lines, so sort first to group duplicates together. The -c flag prepends the count of each line.
Sort by a column
Use -k to sort by a specific field. sort -k2 -n sorts by the second column numerically, which is how you rank table-like output.
Combine with other tools
sort is the perfect end of a pipe: rank the biggest folders with du, list log visitors with awk, then dedupe with uniq. See sort and uniq for details.
Keep learning
Related guides: How to Count and Cut Text with wc and cut · awk: The Linux Text Processing Tool Explained · Linux Redirection and Pipes: A Practical Guide. Read all tutorials on the Learn Linux blog.