The sort Command in Linux is one of the most ignored(?) yet powerful command.
Sort is used to sort data or records.
if you use with other utils like ls it can give you a wide variety of data.
let’s say the ls -l output of a directory is
-rw-r--r-- 1 nandam nandam 785280 2008-05-27 22:26 gdocs-1.0.4.oxt -rw-r--r-- 1 nandam nandam 4997184 2008-05-27 22:16 Sun_ODF_Template_Pack_en-US.oxt -rwxr-xr-x 1 nandam nandam 360756 2008-01-18 19:30 WriterTemplates.oxt -rwxr-xr-x 1 nandam nandam 48019 2008-01-18 19:30 WriterTools.oxt
Now if you want to sort the above data according to column five (size of each file) you have to enter
$ls -l |sort -k 5
and here is what you get
-rwxr-xr-x 1 nandam nandam 360756 2008-01-18 19:30 WriterTemplates.oxt -rwxr-xr-x 1 nandam nandam 48019 2008-01-18 19:30 WriterTools.oxt -rw-r--r-- 1 nandam nandam 4997184 2008-05-27 22:16 Sun_ODF_Template_Pack_en-US.oxt -rw-r--r-- 1 nandam nandam 785280 2008-05-27 22:26 gdocs-1.0.4.oxt
-k —> instructs sort to sort records by column 5 in ascending order.
But, sort considers the fifth column as a text string. to sort the data considering it as a number we must pass -n as below
$ls -l|sort -k 5 -n
and now you will get the correct format as below
-rwxr-xr-x 1 nandam nandam 48019 2008-01-18 19:30 WriterTools.oxt -rwxr-xr-x 1 nandam nandam 360756 2008-01-18 19:30 WriterTemplates.oxt -rw-r--r-- 1 nandam nandam 785280 2008-05-27 22:26 gdocs-1.0.4.oxt -rw-r--r-- 1 nandam nandam 4997184 2008-05-27 22:16 Sun_ODF_Template_Pack_en-US.oxt
But the size is in ascending order. If you want the listing in descending order use -r option
ls -l|sort -k 5 -n -r
or
ls -l|sort -k 5 -nr
now the output will be in descending order like this
-rw-r--r-- 1 nandam nandam 4997184 2008-05-27 22:16 Sun_ODF_Template_Pack_en-US.oxt -rw-r--r-- 1 nandam nandam 785280 2008-05-27 22:26 gdocs-1.0.4.oxt -rwxr-xr-x 1 nandam nandam 360756 2008-01-18 19:30 WriterTemplates.oxt -rwxr-xr-x 1 nandam nandam 48019 2008-01-18 19:30 WriterTools.oxt
anyway the above is only for illustration.. the same can be obtained by command ls
$ls -lS
by default, it sort takes space as delimiter. to set another character as delimiter use -t
$sort -t : xxxxxx
Now you don’t want to sort already sorted data. Use -c to check for sorted data and -u to show only unique records.
-c options report the first line if the given data is not sorted. To stop it from displaying the first record use -C.
sample input files.txt
-rw-r--r-- 1 nandam nandam 4997184 2008-05-27 22:16 Sun_ODF_Template_Pack_en-US.oxt -rw-r--r-- 1 nandam nandam 4997184 2008-05-27 22:16 Sun_ODF_Template_Pack_en-US.oxt -rw-r--r-- 1 nandam nandam 785280 2008-05-27 22:26 gdocs-1.0.4.oxt -rwxr-xr-x 1 nandam nandam 360756 2008-01-18 19:30 WriterTemplates.oxt -rwxr-xr-x 1 nandam nandam 48019 2008-01-18 19:30 WriterTools.oxt
$sort -k 6,7 files.txt
sort the data first by column 6 and then by column 7
and the above command gives the files by last modified date and then by time.
Of course, if you want to list the files by last modification date you can use
$ls -lt
which gives the same output as above command
Let us use another command in conjugation with sort.
like df
$df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/hda1 19542436 4209216 15333220 22% /
tmpfs 242092 0 242092 0% /lib/init/rw
udev 10240 104 10136 2% /dev
tmpfs 242092 0 242092 0% /dev/shm
/dev/hda6 58133024 53965408 4167616 93% /mnt/win
/dev/sdb1 3936736 897564 3039172 23% /media/KINGSTON
/dev/hdc 4588298 4588298 0 100% /media/cdrom0
/dev/sda1 488384000 270011588 218372412 56% /media/disk
$df |sort -k 2 -n
gives the size of partitions in ascending order.
Filesystem 1K-blocks Used Available Use% Mounted on
udev 10240 104 10136 2% /dev
tmpfs 242092 0 242092 0% /dev/shm
tmpfs 242092 0 242092 0% /lib/init/rw
/dev/sdb1 3936736 897564 3039172 23% /media/KINGSTON
/dev/hdc 4588298 4588298 0 100% /media/cdrom0
/dev/hda1 19542436 4209228 15333208 22% /
/dev/hda6 58133024 53965408 4167616 93% /mnt/win
/dev/sda1 488384000 270011588 218372412 56% /media/disk
Suppose if I want to know which drive have largest percent of space used I have to enter
$df | sort -k 5 -n
Filesystem 1K-blocks Used Available Use% Mounted on
tmpfs 242092 0 242092 0% /dev/shm
tmpfs 242092 0 242092 0% /lib/init/rw
udev 10240 104 10136 2% /dev
/dev/hda1 19542436 4209228 15333208 22% /
/dev/sdb1 3936736 897568 3039168 23% /media/KINGSTON
/dev/sda1 488384000 270011588 218372412 56% /media/disk
/dev/hda6 58133024 53965408 4167616 93% /mnt/win
/dev/hdc 4588298 4588298 0 100% /media/cdrom0
and for disk with most free space
$df | sort -k 4 -n
if you want to see processes using large memory use
$ps -A v |sort -k 9 -nr|more
3341 ? S 0:08 76 1001 81054 35696 7.3 python /usr/bin/comix
2674 ? S 0:17 83 1154 79481 32204 6.6 nautilus –no-default-window –sm-client-id default2
3325 ? Sl 0:07 152 58 96405 30412 6.2 /usr/bin/../lib/kompozer/kompozer-bin
2671 ? S 0:07 58 459 36640 20896 4.3 gnome-panel –sm-client-id default1
3141 ? Sl 0:02 56 226 41329 19844 4.0 gnome-terminal
2487 tty7 Ss+ 0:53 65 1591 54696 16044 3.3 /usr/bin/X :0 -audit 0 -auth /var/lib/gdm/:0.Xauth -nolisten tcp vt7
2766 ? Sl 0:00 3 37 35430 14420 2.9 /usr/lib/gnome-applets
--More--
Anyway, you know now how sort is helpful.
above examples are on piping the output of one tool to other tool using ‘ | ‘References