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:

sort -u input.txt > output.txt

Notes

  • The lines in the output.txt file are sorted.

Note on uniq

  • Lots of examples on the web show using sort along with uniq like this:

    sort input.txt | uniq > output.txt

    That does the same thing as as sort -u.

  • The input file has to be sorted first because uniq only compares lines that are on top of each other.
  • Lots of examples on the web show using the -u flag. It removes all instances of the duplicate lines. It's probably not what you want most of the time

    For example, given this already sorted input.txt file:

    alfa
    alfa
    bravo
    charlie
    charlie
    delta

    You get these outputs from the uniq command:

    uniq input.txt
    Output
    alfa
    bravo
    charlie
    delta

    verses with the -u flag:

    uniq -u input.txt
    Output
    bravo
    delta
end of line