Using the Linux sort Command

Linux sort Command on Monitor

The file test6.txt contains a list of names which we want to sort into alphabetical order to make it easier to read. You can see the original file contents in this image.

Linux cat Command Show File Contents

Using the following command, we can display the files contents in alphabetical order.

sort test6.txt

Linux sort Command Showing File Contents

If for some reason we need the contents in reverse alphabetical order then we can use the -r option.

sort test6.txt -r

Linux sort Command Showing Reversed File Contents

We can also redirect the output of the sort command to create a new sorted file.

sort {Original File Name} > {New Sorted File Name}

Other options are shown in the table below.

Option
Meaning

-n

Numeric sort

-c

Check to see if the file is already sorted

-u

Sort and display with duplicates removed

-M

Sort by month

-k

Sort by column number

-o

Output to separate file