Uses of the Linux Sort Command

Items to be sorted must be delimited in some way

Businessman using laptop in modern office
David Lees/Iconica/Getty Images

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.

Basic Rules for Sorting

The sort command rearranges the lines in a text file to sort them 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 that the first letter of other text entries is sorted before text that starts with a letter that appears later in the alphabet.
  • Where text begins with uppercase and lowercase instances of the same letter, the lowercase instance is sorted first.

Sorting a Text File

In order to sort the lines in a delimited Linux file, you use the sort command like this:

sort -k2 table1.txt

which 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 and last names, the sorted output is arranged by the first letter of the first name of each individual in the second column—Elvis, John, Johnny, Justin, Lady, Michael, Mick, and Taylor, 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 Title number 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 -k6 produces a list sorted by price:

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

Reversing a Sort

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

sort -r -k6 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

Saving a Sorted File

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

sort -k6 table1.txt > test_new.txt

where "test_new.txt" is the new file.

Sorting a Stream Output

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

ls -n | sort -k5

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