Sort Command in Linux with Examples

The sort command is used in Linux and Unix system to arrange the contents of the file in a specific order. Using sort command you order lines, alphabet and numeric in a file.

Sort command has many useful options to reverse order, sort a file numerically, sorting a table by column number, checks if a file is already sorted and removes duplicates.

In this tutorial, we learn about sort command in Linux with some practical examples.

Sort Command and its syntax

The following line shows the sort command syntax:

sort [options] [files]

It takes input from STDIN by default but can sort files when file names are provided as an argument and it can also sort numerically as well.

Sort command can be invoked by typing just sort in the terminal. It will then prompt for the input from STDIN. After entering STDIN, ctrl+d is entered for marking end of input.

The following output shows the input data from STDIN is rearranged in alphabetical order.

$ sort
cappa
beta
delta
theta
alpha

alpha
beta
cappa
delta
theta

Sort a file

By default, sort treats all the characters as string characters. The numeric characters are also treated as characters.

First, we create a file with numeric data to be sorted. The following command creates a file 'numeric.txt' with numeric data:

$ cat > numeric.txt
01
10
25
83
502
111

Now, let us sort the file 'numeric.txt' as follows:

$ sort numeric.txt
01
10
111
25
502
83

The output shown above is not what we generally expect. Of course, 111 is larger than 25 and 502 is greater than 83 as well. But if you see closely, the numbers are sorted in dictionary order.

For example, for 111 and 25, compare the first character, 1 precedes 2. Hence 111 is placed before 25.

Let's now check how it sorts when content is alphabets or words. The following file 'wordlist.txt' contain words starting with uppercase case and lower case and display sorted list:

$ sort wordlist.txt
Aeroplane
ant
apple
bark
born
Box
Cat
tom
welcome

Sort options

Sort command comes with pretty good options to order file contents. Let's discuss some of them as follows:

sort -n: To sort a file numerically.
sort -u: Suppress lines that repeat an earlier key.
sort -k: Sorting a table by column number.
sort -t SEP: Use the provided separator to identify the fields.
sort -M: Sort as per the calendar month specified.
sort -b: Ignore blanks at the start of the line.
sort -r: Sorting in reverse order.
sort -o: Output to a new file.

Sort file numerically (-n Option)

If we want to sort the contents inside a file numerically, we use -n option.

In the following example, the file 'numeric.txt' contains numbers and data is sorted in numerical order:

$ sort -n numeric.txt
01
10
25
83
111
502

To reverse the number in the file, you use a combination of -nr option.

For example:

$ sort -nr numeric.txt
502
111
83
25
10
01

Sort and remove duplicate lines (-u Option)

To sort and remove duplicate lines from a file use -u option. The first command will list the content of the file duplicate.txt using cat command and second we will use -u option to remove duplicate lines.

Example:

$ cat duplicate.txt
hello
linux
lInux
Linux
raghu
world
zzz
zzz

Sample output where you can see the duplicate lines are removed 'duplicate.txt' file:

$ sort -u duplicate.txt
hello
linux
lInux
Linux
raghu
world
zzz

Sort and Ignore Case (-f Option)

Like many other Linux and Unix tools, sort command is case sensitive by default. But if we need to ignore the case, we can use -f or --ignore-case option.

For example, in the 'duplicate.txt', there are three instances of the word 'linux' in different cases. By default, they all are printed with -u option. It means they are not unique for sort command.

But if we use -f option it would be ignored as shown below:

$ sort -f -u duplicate.txt
hello
linux
raghu
world
zzz

Sort by column (-k Option)

The sort command can sort any table using column number using -k option. For example use '-k3' to sort third column in the table.

Let consider we have a table of contents in 'population.txt' file as follows:

$ cat population.txt
Kids 500 India
Youth 400 England
Senior 600 USA
Junior 9000 Australia
Pensioners 650 China

In the following we sort the table using column number 2 in the population.txt file:

$ sort -k2 population.txt
Youth 400 England
Kids 500 India
Senior 600 USA
Pensioners 650 China
Junior 9000 Australia

Sort multiple columns

In some situations, you may need to sort a table using multiple columns. Let's take an example of a file containing multiple columns of data.

Following the file 'columns.txt'contain multiple columns as listed:

cat columns.txt
version1.2    10  25
version1.2    30  50
version1.1    10  30
version1.1    40  50
version1.2    40  50
version1.1    10  20
version1.1    5   8

Now check the command to sort columns 1, 2 and 3 in numerical order.

$ sort -k1,1 -k2,2n -k3,3n columns.txt
version1.1    5   8
version1.1    10  20
version1.1    10  30
version1.1    40  50
version1.2    10  25
version1.2    30  50
version1.2    40  50

Sort by column field separator (delimiter -t Option)

By default, delimiter for columns is whitespace or tab. But we can use custom delimiter using -t or --field-separator option in sort.

Let's take an example, use colon (:) as the delimiter and '/etc/passwd' file to sort.

If we want to sort /etc/passwd file on the basis of UIDs, we use the following command:

$ sort -n -t ':' -k3 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
news:x:9:13:news:/etc/news:
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
<---output truncated--->

Sort by month (-M Option)

Using -M option you can sort by month. If you have a file with months, sort command order by months.

For example, 'months.txt' file contain a list of month names.

$ cat months.txt
sep
august
july
dec
feb
may
jan

We will now use -M option to sort 'months.txt' file by month.

$ sort -M months.txt
jan
feb
may
july
august
sep
dec

Sort human readable numbers (-h Option)

Sort can order human-readable numeric like 2K, 5G, 3M using -h or --human-numeric-sort option.

For example, below file list human readable number

$ cat human_numeric.txt
4G
2K
3M
1G
34K
52M
200M

Following command use -h option to compare and sort human-readable numbers.

$ sort -h human_numeric.txt
2K
34K
3M
52M
200M
1G
4G

Check for sorted input (-c Option)

The sort command can check if the input is already sorted or not using -c option. If it is not sorted, it returns the first unsorted line.

$ sort -c /etc/passwd
sort: /etc/passwd:2: disorder: daemon:x:1:1:daemon:/usr/sbin:/bin/sh

Nothing is printed for sorted input:

$ sort /etc/passwd | sort -c
$ echo $?
0

Sort with other commands

The sort is a text processing tool, so it can be used by piping in some input from another command.

In the following command we use tail command output as input for sort:

$ tail /etc/passwd | sort
altair:x:1001:1001:Altair Ibn La Ahad,,,,:/home/altair:/bin/bash
bind:x:120:132::/var/cache/bind:/bin/false
dnsmasq:x:121:65534:dnsmasq,,,:/var/lib/misc:/bin/false
mysql:x:115:129:MySQL Server,,,:/nonexistent:/bin/false
postfix:x:105:126::/var/spool/postfix:/bin/false
raghu:x:1000:1000:raghu,,,:/home/raghu:/bin/bash
smmsp:x:119:131:Mail Submission Program,,,:/var/lib/sendmail:/bin/false
smmta:x:118:130:Mail Transfer Agent,,,:/var/lib/sendmail:/bin/false
sshd:x:117:65534::/var/run/sshd:/usr/sbin/nologin
statd:x:116:65534::/var/lib/nfs:/bin/false

The sorting can be reversed with -r option.

Lets take another example to sort du command output in human-readable format by size, use the following command:

$ du -h | sort -h

Conclusion

In this tutorial, we learn about Linux sort and its option to order file contents. I hope you enjoyed reading and please comment below if you find any other examples.

Raghu 8:11 am

Comments

Your email address will not be published. Required fields are marked *