Uses of the Linux Sort Command

Items to be sorted must be delimited in some way

Data in a Linux text file can be sorted with the sort command as long as each element is delimited in some way. Frequently, the comma is used as the separator for delimited information.

Business analyst working at a laptop
David Lees / Iconica / Getty Images

Basic Rules for Sorting

The sort command rearranges the lines in a text file to sort the lines numerically and alphabetically. The default rules for the sort command are:

  • Text that starts with a number appears before text that starts with a letter; lower numbers sort first.
  • Text that starts with a letter that appears in the alphabet earlier than the first letter of other text entries is sorted before the text that starts with a letter that appears later in the alphabet.
  • When text begins with uppercase and lowercase instances of the same letter, the lowercase instance is sorted first.

Sort a Text File

To sort the lines in a delimited Linux file, use the sort command like this:

sort -k2 table1.txt

This command sorts the file table1.txt according to the characters starting at the second column (k2 refers to the second column). Assuming the input file content is:

1, Justin, Timberlake, Title 545, Price $7.30
2, Taylor, Swift, Title 723, Price $7.90
3, Mick, Jagger, Title 610, Price $7.90
4, Lady, Gaga, Title 118, Price $7.30
5, Johnny, Cash, Title 482, Price $6.50
6, Elvis, Presley, Title 335, Price $7.30
7, John, Lennon, Title 271, Price $7.90
8, Michael, Jackson, Title 373, Price $5.50

Because the second column in this example contains first names, the sorted output is arranged by the first letter of the first name of each individual in the second column, as shown below:

6, Elvis, Presley, Title 335, Price $7.30
7, John, Lennon, Title 271, Price $7.90
5, Johnny, Cash, Title 482, Price $6.50
1, Justin, Timberlake, Title 545, Price $7.30
4, Lady, Gaga, Title 118, Price $7.30
8, Michael, Jackson, Title 373, Price $5.50
3, Mick, Jagger, Title 610, Price $7.90
2, Taylor, Swift, Title 723, Price $7.90

If you sort the file with -k3 (using the line contents starting at column 3, the Last Name column), the output is:

5, Johnny, Cash, Title 482, Price $6.50
4, Lady, Gaga, Title 118, Price $7.30
8, Michael, Jackson, Title 373, Price $5.50
3, Mick, Jagger, Title 610, Price $7.90
7, John, Lennon, Title 271, Price $7.90
6, Elvis, Presley, Title 335, Price $7.30
2, Taylor, Swift, Title 723, Price $7.90
1, Justin, Timberlake, Title 545, Price $7.30

and -k5 produces a list sorted by price:

4, Lady, Gaga, Title 118, Price $7.30
7, John, Lennon, Title 271, Price $7.90
6, Elvis, Presley, Title 335, Price $7.30
8, Michael, Jackson, Title 373, Price $5.50
5, Johnny, Cash, Title 482, Price $6.50
1, Justin, Timberlake, Title 545, Price $7.30
3, Mick, Jagger, Title 610, Price $7.90
2, Taylor, Swift, Title 723, Price $7.90

Wait, that didn't work, did it? Instead of sorting by price, the command sorted the list by the title number. Why? Well, unless it's given the delimiter at which it should break each line into columns, it uses spaces. Since there's a space in the middle of each title entry, column four in each entry becomes the word, "Title," and column five becomes the title number.

Use the -t flag to specify the delimiter for sort. This controls exactly where the command separates out columns.

sort -t, -k5 table1.txt

This produces the desired result:

8, Michael, Jackson, Title 373, Price $5.50
5, Johnny, Cash, Title 482, Price $6.50
1, Justin, Timberlake, Title 545, Price $7.30
4, Lady, Gaga, Title 118, Price $7.30
6, Elvis, Presley, Title 335, Price $7.30
2, Taylor, Swift, Title 723, Price $7.90
3, Mick, Jagger, Title 610, Price $7.90
7, John, Lennon, Title 271, Price $7.90

Usually, you'll want to specify the delimiter. It prevents unwanted confusion and inaccurate results. This is especially true with larger files that might not always be obviously incorrect.

Reverse a Sort

The -r option reverses the sorting. For example, using the results above:

sort -r -t, -k5 table1.txt

yields:

7, John, Lennon, Title 271, Price $7.90
3, Mick, Jagger, Title 610, Price $7.90
2, Taylor, Swift, Title 723, Price $7.90
6, Elvis, Presley, Title 335, Price $7.30
4, Lady, Gaga, Title 118, Price $7.30
1, Justin, Timberlake, Title 545, Price $7.30
5, Johnny, Cash, Title 482, Price $6.50
8, Michael, Jackson, Title 373, Price $5.50

Save a Sorted File

Sorting a file doesn't save it. To save the sorted list in a file, use the redirect operator:

sort -k6 table1.txt > test_new.txt

where test_new.txt is the new file.

Sort a Stream Output

You can also apply the sort command to the output of a stream, such as the pipe operator:

ls -n | sort -t, -k5

This sorts the output of the file listing generated by the ls command by file size, starting with the largest files. The -n operator specifies numeric sorting rather than alphabetic.