Deduplicate Lines in a File on the Command Line
September 2025
This is what I use to remove duplicate lines from a text file on the command line:
Notes
-
The lines in the
output.txtfile are sorted.
Note on uniq
-
Lots of examples on the web show using
sortalong withuniqlike this:|That does the same thing as as
sort -u. -
The input file has to be sorted first because
uniqonly compares lines that are on top of each other. -
Lots of examples on the web show using the
-uflag. It removes all instances of the duplicate lines. It's probably not what you want most of the timeFor example, given this already sorted input.txt file:
alfa alfa bravo charlie charlie delta
You get these outputs from the
uniqcommand:verses with the
-uflag:
end of line