×


Sort command in Ubuntu Linux with examples

Are you in search for a command line utility to sort content in text files?

Look no further than Sort, a tool specifically built for this purpose. 

The sort command is one of the GNU core utilities that is used to sort content in a particular order. It can sort the content in a number of ways that include alphabetically, numerically, by month, in reverse order  and so on.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform Linus related queries.

In this context, you will learn the basic usage and some common options of the sort command in Ubuntu Linux.

Also, you can apply the same commands on other Linux distributions.


More about the Sort Command

You need to note the following when dealing with the sort command:

i. Digits appear first before letters

ii. For each letter, lowercase are sorted first.

iii. By default, spaces are used as a field separator. Other field separators can be used as well.


The syntax of the sort command is as follows:

$ sort [OPTION]... [FILE]...


How to use the Sort Command ?

You can use the sort command without any command-line options sorts the content of a specified file alphabetically.

If we have an example.txt file which contains some items like:

$ cat example.txt
book
pen
groceries
Apple
mango

Then, to sort the content of the example.txt file, the command would be:

$ sort example.txt

The output will sort out the file in an alphabetic order.


Also, If the file contains both alphabets and numbers, then the sort command will sort numbers before alphabets. 

Below is our sample file which contains both numbers and alphabets: 

$ cat example.txt
book
pen
1
groceries
2
4
apple
mango

When you run the sort command as per the items above,

Then the output of the above file after sorting will show that the numbers are sorted before the alphabets as seen below:

$ sort example.txt
1
2
4
apple
book
groceries
mango
pen


Sort Command Options

The sort command provides a lot of options that expand its usefulness. Let's have a look at few of the options and how to use them.


1. Sort in Reverse Order

To sort the file content in reverse order, use the -r option. For example, below is the content of our sample file which we want to sort in reverse alphabetical order:

$ cat example.txt
book
pen
groceries
apple
mango

To sort the content of this file in reverse order, use the -r option as follows:

$ sort -r example.txt
pen
mango
groceries
book
apple


2. Sort in Numeric Order

To sort the data numerically, use the -n option with the sort command. This option is useful if your file contains numbers and you want to sort them from lowest number to highest or from highest to lowest. Keep in mind that in default sorting, 113 would be considered smaller than 2.

Below is an example of a default sorting of our sample file. Keep in mind that in default sorting, sorting is done on the basis of the first number. 

If the first number is the same, then it sorts on the basis of the second number. Like in the below example, 113 is smaller than 13 and 2.

$ sort example.txt
1
113
13
2
200
3
30

To sort the above content numerically (from lowest number to highest), use the -n option as follows:

$ sort -n example.txt
1
2
3
13
30
113
200


3. Sort by Month

The default sort command sorts the file content alphabetically. If a file contains a list of month’s names, you can sort them month-wise using the -M command.

For example, below is our sample file containing the month names:

cat example.txt
march
july
jan
aug
june
sep
feb
december

To sort the above file content by months, use the -M option as follows:

$ sort -M example.txt
jan
feb
march
june
july
aug
sep
december


4. Sort on the Basis of Column Number

By default, the sort command starts sorting the content on the basis of the first character of the first column.

For example, below is our sample file which contains two columns; fruit names and their respective prices. If we use default sorting, it would be done based on the first column (containing fruit names):

$ cat example.txt
mango 400
apple 300
orange 500
banana 50
pineapple 1000
guava 600

To sort the content of the above file on the basis of the second column(prices), we will have to use the -k command as follows:

$ sort -k2 -n example.txt

Where -k2 tells the sort command to sort on the second column and -n is used for numerical sort.

The command will sort the content based on their prices (from lower to higher):

$ sort -k2 -n example.txt
banana 50
apple 300
mango 400
orange 500
guava 600
pineapple 1000


5. Use Different Field Separator

By default, the sort command uses space as a field separator. To use a different field separator other than space, use the -t option. 

Below is our sample file that we want to sort on the basis of the second column (separated by commas):

$ cat example.txt
mango, 400
apple, 300
orange, 500
banana, 50
pineapple, 1000
guava, 600

The command to sort the above file on the basis of the second column would be:

$ sort -k2 -n -t "," example.txt

Where -k2 tells the sort command to sort on the second column, -n is used for numerical sort and -t is used to specify the field separator.

$ sort -k2 -n -t "," example.txt
banana, 50
apple, 300
mango, 400
orange, 500
guava, 600
pineapple, 1000


6. Sort and Remove Duplicates

The -u option with sort command also removes the duplicates along with sorting the content. Below is our sample file which contains the duplicate values:

$ cat example.txt
mango
apple
orange
banana
mango
pineapple
guava

To sort this file alphabetically and remove the duplicate values as well, the command would be:

$ sort -u example.txt

This is the output of the sorted file with removed duplicates.

$ sort -u example.txt
apple
banana
guava
mango
orange
pineapple


7.  Check for Sorted Input

Using the -c option with the sort command, you can check if a file is earlier sorted or not. If a file is earlier sorted, you will see no output. 

However, if the file is not sorted, it will show the lines which are out of order.

Here is our sample file:

$ cat example.txt
apple
apple
APPLE

To check whether this file is already sorted or not, use the -c option as follows:

$ sort -c example.txt

As our sample file was already sorted, therefore no output is displayed.

$ sort -c example.txt
$


8. Save Sorted Output in a File

Sort command only displays the sorted output on Terminal; it does not save it. To save the sorted output in a separate file, use the redirect operator. 

For example, to save the sorted output of file.txt in a new file named sortedfile.txt, use the below command:

$ sort file.txt > sortedfile.txt


9. Sort Command using Pipe Option

You can sort the output of other commands by piping them to the sort command.

For example, we want to numerically sort a file/directory listing in the Home directory by their sizes. 

We can do so by using the ls command and piping its output to sort the command as follows.

$ ls -l /home/linuxapt/ | sort -nk5

In the output of the above command, you will see files are sorted by their sizes.


[Need urgent assistance in fixing missing packages on Debian Linux Systems? We are available to help you. ]


Conclusion

This article covers the basic syntax and usage of the sort command in Ubuntu Linux. To view help and learn about more sort options, visit the sort man page  or type sort –help in Terminal.

sort is a simple and very useful command which will rearrange the lines in a text file so that they are sorted, numerically and alphabetically. 


By default, the rules for sorting are:

1. Lines starting with a number will appear before lines starting with a letter.

2. Lines starting with a letter that appears earlier in the alphabet will appear before lines starting with a letter that appears later in the alphabet.

3. Lines starting with a lowercase letter will appear before lines starting with the same letter in uppercase.