Show unique list elements with the Linux sort -u command

If you're ever working on a Unix or Linux system, and have a list with duplicated items in it, and want a smaller list of only the unique items in the list, the sort command is your friend.

I just ran into a situation where I generated a list of fonts on a Mac OS X (Unix) system, and my list ended up with a bunch of duplicated names, like this:

AScore
AScore
AScore
Abadi MT Condensed Light
Abadi MT Condensed Light
Academy Engraved LET Fonts
Academy Engraved LET Fonts
AlBayan
AlBayan
AlBayan
Andale Mono
Andale Mono

Assuming that list is in a file named "fonts.txt", I can easily output a list of unique, sorted list elements with this Linux sort -u command:

$ sort -u < fonts.txt

AScore
Abadi MT Condensed Light
Academy Engraved LET Fonts
AlBayan
Andale Mono

As you can see, only the unique items from the original list are shown. Also, had my list been originally unsorted, you would have seen that the sort command also does what its name implies, sorting the list.

As a final note, if you want to write the unique, sorted elements to a new file named "fonts.sorted", you can use this command, redirecting both standard input and standard output:

$ sort -u < fonts.txt > fonts.sorted

That's all you have to do to create a unique, sorted list using the Unix/Linux sort command.

Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.