linux beginner
Now Reading
Sort Command in Linux with Examples
0

Sort Command in Linux with Examples

Sort Command

In this article we will cover about various Sort command used in Linux. It is used to print lines of input text files in sorted order.

To create a text file

Options
-e enables interpretation of backslash
/n tells echo to write each string to new line

[user1@linuxhelp Desktop]$ echo -e "centos\nredhat\ndebian\nlinuxmint\narch\nubuntu\nfedora\nopensuse\nmageia\nmanjaro" > os.txt

To look at the contents of file

[user1@linuxhelp Desktop]$ cat os.txt
centos
redhat
debian
linuxmint
arch
ubuntu
fedora
opensuse
mageia
manjaro

To sort contents of file

The following sort command is used to sort contents of file.

[user1@linuxhelp Desktop]$ sort os.txt 
arch
centos
debian
fedora
linuxmint
mageia
manjaro
opensuse
redhat
ubuntu

It shows the sorted output of the file in the terminal.

To verify the Contents

Redirect the os.txt file to sorted.txt file. Now use cat command to verify the Contents of file.

[user1@linuxhelp Desktop]$ sort os.txt > sorted.txt
[user1@linuxhelp Desktop]$ cat sorted.txt 
arch
centos
debian
fedora
linuxmint
mageia
manjaro
opensuse
redhat
ubuntu

To sort in reverse order

The following sort command is used to sort in reverse order. -r switch is used to redirect output of a file in reverse order.

[user1@linuxhelp Desktop]$ sort -r os.txt > rsorted.txt
[user1@linuxhelp Desktop]$ cat rsorted.txt 
ubuntu
redhat
opensuse
manjaro
mageia
linuxmint
fedora
debian
centos
arch

To create new file in same location

To list
ls -l is used to list the output in the home directory.


[user1@linuxhelp Desktop]$ ls -l /home/user1/Documents/ > /home/user1/Desktop/new.txt
[user1@linuxhelp Desktop]$ cat new.txt 
total 24
drwxrwxr-x 2 user1 user1 4096 Mar  9 15:52 dir5
drwxrwxr-x 2 user1 user1 4096 Mar  9 15:52 dir6
drwxrwxr-x 2 user1 user1 4096 Mar  9 15:52 dir7
-rw-rw-r-- 1 user1 user1    0 Mar  9 15:52 file1
-rw-rw-r-- 1 user1 user1   62 Mar  9 15:53 file2
-rw-rw-r-- 1 user1 user1    0 Mar  9 15:52 file3
-rw-rw-r-- 1 user1 user1   23 Mar  9 15:53 file4
drwxrwxr-x 2 user1 user1 4096 Mar  9 15:53 test

To sort contents of file ‘new.txt‘ on the basis of 2nd column

The following sort command -k is used to sort a file on basis of the column.

Example
Based on 2nd column

[user1@linuxhelp Desktop]$ sort -nk2 new.txt 
-rw-rw-r-- 1 user1 user1    0 Mar  9 15:52 file1
-rw-rw-r-- 1 user1 user1    0 Mar  9 15:52 file3
-rw-rw-r-- 1 user1 user1   23 Mar  9 15:53 file4
-rw-rw-r-- 1 user1 user1   62 Mar  9 15:53 file2
drwxrwxr-x 2 user1 user1 4096 Mar  9 15:52 dir5
drwxrwxr-x 2 user1 user1 4096 Mar  9 15:52 dir6
drwxrwxr-x 2 user1 user1 4096 Mar  9 15:52 dir7
drwxrwxr-x 2 user1 user1 4096 Mar  9 15:53 test
total 24

-n option is used to specify the integers in the specified column.

Based on 9th column

[user1@linuxhelp Desktop]$ sort -k9 new.txt 
total 24
drwxrwxr-x 2 user1 user1 4096 Mar  9 15:52 dir5
drwxrwxr-x 2 user1 user1 4096 Mar  9 15:52 dir6
drwxrwxr-x 2 user1 user1 4096 Mar  9 15:52 dir7
-rw-rw-r-- 1 user1 user1    0 Mar  9 15:52 file1
-rw-rw-r-- 1 user1 user1   62 Mar  9 15:53 file2
-rw-rw-r-- 1 user1 user1    0 Mar  9 15:52 file3
-rw-rw-r-- 1 user1 user1   23 Mar  9 15:53 file4
drwxrwxr-x 2 user1 user1 4096 Mar  9 15:53 test

To pipeline it directly on the terminal with actual command

To list the contents in the documents folder directly in sorted order follow the below command.

[user1@linuxhelp Desktop]$ ls -l /home/user1/Documents | sort -nk5
-rw-rw-r-- 1 user1 user1    0 Mar  9 15:52 file1
-rw-rw-r-- 1 user1 user1    0 Mar  9 15:52 file3
total 24
-rw-rw-r-- 1 user1 user1   23 Mar  9 15:53 file4
-rw-rw-r-- 1 user1 user1   62 Mar  9 15:53 file2
drwxrwxr-x 2 user1 user1 4096 Mar  9 15:52 dir5
drwxrwxr-x 2 user1 user1 4096 Mar  9 15:52 dir6
drwxrwxr-x 2 user1 user1 4096 Mar  9 15:52 dir7
drwxrwxr-x 2 user1 user1 4096 Mar  9 15:53 test

To sort and remove duplicates

In os.txt file “arch” is repeated twice using -u option we can remove the duplicate one.

[user1@linuxhelp Desktop]$ cat os.txt 
centos
arch
redhat
debian
linuxmint
arch
ubuntu
fedora
opensuse
mageia
manjaro
redhat
[user1@linuxhelp Desktop]$ sort -u os.txt 
arch
centos
debian
fedora
linuxmint
mageia
manjaro
opensuse
redhat
ubuntu

To sort contents of two files in single output

To sort and display the contents of os.txt file and new.txt file in single output.

[user1@linuxhelp Desktop]$ sort os.txt new.txt 
arch
arch
centos
debian
drwxrwxr-x 2 user1 user1 4096 Mar  9 15:52 dir5
drwxrwxr-x 2 user1 user1 4096 Mar  9 15:52 dir6
drwxrwxr-x 2 user1 user1 4096 Mar  9 15:52 dir7
drwxrwxr-x 2 user1 user1 4096 Mar  9 15:53 test
fedora
linuxmint
mageia
manjaro
opensuse
redhat
-rw-rw-r-- 1 user1 user1    0 Mar  9 15:52 file1
-rw-rw-r-- 1 user1 user1    0 Mar  9 15:52 file3
-rw-rw-r-- 1 user1 user1   23 Mar  9 15:53 file4
-rw-rw-r-- 1 user1 user1   62 Mar  9 15:53 file2
total 24
ubuntu

To sort, merge and remove duplicates from these two files

To sort, merge and display the contents of os.txt file and new.txt file in single output.

[user1@linuxhelp Desktop]$ sort -u os.txt new.txt
arch
centos
debian
drwxrwxr-x 2 user1 user1 4096 Mar  9 15:52 dir5
drwxrwxr-x 2 user1 user1 4096 Mar  9 15:52 dir6
drwxrwxr-x 2 user1 user1 4096 Mar  9 15:52 dir7
drwxrwxr-x 2 user1 user1 4096 Mar  9 15:53 test
fedora
linuxmint
mageia
manjaro
opensuse
redhat
-rw-rw-r-- 1 user1 user1    0 Mar  9 15:52 file1
-rw-rw-r-- 1 user1 user1    0 Mar  9 15:52 file3
-rw-rw-r-- 1 user1 user1   23 Mar  9 15:53 file4
-rw-rw-r-- 1 user1 user1   62 Mar  9 15:53 file2
total 24
ubuntu

To create a text file ‘month.txt’

[user1@linuxhelp Desktop]$ echo -e "march\napril\njuly\njune\njan\noct" > month.txt
[user1@linuxhelp Desktop]$ cat month.txt 
march
april
july
june
jan
oct

To sort the file ‘month.txt‘ on the basis of month order

Use switch -M to sort the file ‘month.txt‘ on the basis of month order -month-sort

[user1@linuxhelp Desktop]$ sort -M month.txt 
jan
march
april
june
july
oct

To check if files are sorted or not using sort command

Using -c option you can check if the file is sorted or not. If it is sorted it won’t display any output

[user1@linuxhelp Desktop]$ sort -c sorted.txt

Or else it will display the disordered line number.
[user1@linuxhelp Desktop]$ sort -c os.txt 
sort: os.txt:2: disorder: arch

To Sort on the basis of column

For contents separated by anything other than space like ‘|’ or ‘\’ or ‘+’ or ‘.’

[user1@linuxhelp Desktop]$ echo -e "34+windows+linux+unix\n45+mageia+opensuse+redhat\n546+mongodb+tomcat+perl\n5+python+java+php\n368+zimbra+lotus+gmail" > linux.txt
[user1@linuxhelp Desktop]$ cat linux.txt 
34+windows+linux+unix
45+mageia+opensuse+redhat
546+mongodb+tomcat+perl
5+python+java+php
368+zimbra+lotus+gmail

To sort file on the basis of 1st field

The following sort command is used to sort file on the basis of 1st field.

[user1@linuxhelp Desktop]$ sort -t '+' -nk1 linux.txt 
5+python+java+php
34+windows+linux+unix
45+mageia+opensuse+redhat
368+zimbra+lotus+gmail
546+mongodb+tomcat+perl

On 4th field basis which is non numeric

[user1@linuxhelp Desktop]$ sort -t '+' -k4 linux.txt 
368+zimbra+lotus+gmail
546+mongodb+tomcat+perl
5+python+java+php
45+mageia+opensuse+redhat
34+windows+linux+unix

To sort the contents of ‘ls -l’ command for your home directory

On basis of 5th column
It returns the values based on the amount of data in random order.

[user1@linuxhelp Desktop]$ ls -l /home/user1/Documents/ | sort -k5 -R
drwxrwxr-x 2 user1 user1 4096 Mar  9 15:52 dir5
total 24
-rw-rw-r-- 1 user1 user1   62 Mar  9 15:53 file2
-rw-rw-r-- 1 user1 user1    0 Mar  9 15:52 file1
drwxrwxr-x 2 user1 user1 4096 Mar  9 15:52 dir6
drwxrwxr-x 2 user1 user1 4096 Mar  9 15:53 test
-rw-rw-r-- 1 user1 user1    0 Mar  9 15:52 file3
-rw-rw-r-- 1 user1 user1   23 Mar  9 15:53 file4
drwxrwxr-x 2 user1 user1 4096 Mar  9 15:52 dir7

Whereas if you run the same command next time it won’t return the same output

[user1@linuxhelp Desktop]$ ls -l /home/user1/Documents/ | sort -k5 -R
-rw-rw-r-- 1 user1 user1   62 Mar  9 15:53 file2
-rw-rw-r-- 1 user1 user1    0 Mar  9 15:52 file3
total 24
-rw-rw-r-- 1 user1 user1    0 Mar  9 15:52 file1
drwxrwxr-x 2 user1 user1 4096 Mar  9 15:52 dir5
-rw-rw-r-- 1 user1 user1   23 Mar  9 15:53 file4
drwxrwxr-x 2 user1 user1 4096 Mar  9 15:52 dir7
drwxrwxr-x 2 user1 user1 4096 Mar  9 15:53 test
drwxrwxr-x 2 user1 user1 4096 Mar  9 15:52 dir6

To override the default sorting preference

Before the override option is to be used we have to export the environment variable.

[user1@linuxhelp Desktop]$ export LC_ALL=C
[user1@linuxhelp Desktop]$ sort os.txt 
Redhat
arch
centos
debian
fedora
linuxmint
mageia
manjaro
opensuse
ubuntu

Here, it overrides the uppercase letter as the first one.

Use option ‘-f‘ aka ‘–ignore-case‘ to get organized output.

[user1@linuxhelp Desktop]$ sort -f os.txt 
arch
centos
debian
fedora
linuxmint
mageia
manjaro
opensuse
Redhat
ubuntu

So it just sort and won’t consider the lower and uppercase letters.

To run sort on two input file and merge

The following sort command is used to Create two text files and add some data and use cat command to check contents of the file.

[user1@linuxhelp Desktop]$ echo -e “1 redhat\n3 centos\n2 fedora” > file1.txt
[user1@linuxhelp Desktop]$ cat file1.txt
1 redhat
3 centos
2 fedora

Add 2nd file with some data

[user1@linuxhelp Desktop]$ echo -e “3 debian\n2 mageia\n1 ubuntu” > file2.txt
[user1@linuxhelp Desktop]$ cat file2.txt
3 debian
2 mageia
1 ubuntu

To merge output of both files

[user1@linuxhelp Desktop]$ join <(sort  file1.txt) <(sort file2.txt)
1 redhat ubuntu
2 fedora mageia
3 centos debian

Thank you! for using Linux Help.

You find this tutorial helpful? Share with your friends to keep it alive.

For more help topics browse our website www.linuxhelp.com

Be the first to comment, we value your suggestions. For further queries please comment below.

Leave a Response