How to Use the Linux sort Command to Alphabetize Text Files

The Chaos of Unstructured Data

When working in the Linux terminal, you will frequently deal with massive amounts of unstructured text. This could be a list of 5,000 email addresses dumped from a database, a server log file containing hundreds of IP addresses, or a simple text document containing a list of server hostnames.

Reading through a randomized list to find duplicates or locate a specific entry is incredibly inefficient. Before you can analyze text data properly, you usually need to organize it.

The Linux sort command is a powerful, built-in utility designed to take any text input—whether from a file or piped from another command—and instantly organize it logically.

Step 1: The Basic Alphabetical Sort

Imagine you have a file named employees.txt containing a randomized list of 100 first names.

To sort the file alphabetically from A to Z, simply type:

sort employees.txt

The terminal will instantly output the perfectly alphabetized list to your screen.

Important Note: By default, sort only outputs the result to the screen (standard output); it does not modify your original employees.txt file. If you want to save the sorted list, you must redirect the output into a new file using the > operator:

sort employees.txt > sorted_employees.txt

Step 2: Reverse Sorting

To sort the list in reverse alphabetical order (Z to A), use the -r (reverse) flag.

sort -r employees.txt

Step 3: Sorting Numbers (The Numeric Flag)

A common mistake when using the sort command is attempting to sort a file containing numbers (like a list of invoice amounts in prices.txt) without providing any flags.

By default, sort treats everything as text. If you have the numbers 2, 15, and 100, a basic sort will output:

  • 100
  • 15
  • 2

It does this because the character “1” comes before the character “2” in the alphabet, entirely ignoring the mathematical value of the number.

To force the command to evaluate the data as actual math, you must use the -n (numeric) flag.

sort -n prices.txt

This will correctly output 2, 15, and 100.

Step 4: Sorting by Specific Columns

The true power of sort is revealed when dealing with structured data, like a CSV file or a space-separated log file.

Imagine a file named sales.txt formatted like this:
John 500
Sarah 1200
Mike 300

If you run a basic sort sales.txt, it will sort alphabetically by the first column (the names). What if you want to find out who sold the most? You need to sort by the second column (the numbers).

You can target specific columns using the -k (key) flag. To sort numerically by the second column, run:

sort -n -k 2 sales.txt

Output:
Mike 300
John 500
Sarah 1200

Step 5: Removing Duplicates

Finally, sort is incredibly useful for cleaning dirty data. If you have a massive list of email addresses and you know there are duplicates scattered throughout, you can sort the list and instantly delete any redundant entries using the -u (unique) flag.

sort -u emails.txt > clean_emails.txt

The resulting clean_emails.txt file will be perfectly alphabetized, and every email address will appear exactly one time.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.