Hosting or Servers Logo

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

You Are Leaving Us

This link is being provided as a convenience and for informational purposes only it is not an endorsement or an approval of any of the products, services or opinions of the corporation or organization or individual.

Hostingorservers.com bears no responsibility for the accuracy, legality or content of the external site or for that of subsequent links.