How to Remove Duplicate Lines in Linux (uniq)
uniq filters out adjacent duplicate lines from sorted input, leaving only unique lines.
Published October 2, 2026
uniq filters out adjacent duplicate lines from sorted input, leaving only unique lines.
Key points
- uniq only removes duplicates that are directly adjacent — sort the input first for it to work reliably: sort file.txt | uniq
- uniq -c prefixes each line with a count of how many times it occurred consecutively
- uniq -d shows only the lines that had duplicates, which is useful for finding repeated values
Example
sort names.txt | uniq -c