Linux provides a lot of useful tools for system administrators. These tools gives the administrator a developer power so the administrator can easily change, modify, filter, sort the data.
sort is an other little but powerful tool. In this tutorial we will look different usage examples of sort.
We will use following simple example file named
apple 1 good JAN grape 5 bad DEC banana 2 good MAR melon 10 bad JUN
sort command will use following syntax.
sort [OPTION]... [FILE]... sort [OPTION]... --files0-from=F
Sort can be used with file or standard input. In this example we will sort file content according to each lines start. As we see our file content is sorted alphabetically.
$ sort fruits.txt
By default sort is done ascending. Some time it can be useful to sort reverse or descending mode. This can be done like below by providing
$ sort -r fruits.txt
Add Output To File
After sort operation the new sorted content is printed to the standard output. One of the alternative way to save output is writing to a file.
$ sort -r fruits.txt > reverse_fruits.txt
Sort is by default case sensitive operation which means while sorting character case upper/lower is detected and sorted accordingly. If this is not suits you it can be disabled with
$ sort -f fruits.txt
Check Input File If Sorted
Another useful feature of sort is check the status of files if it is already sorted. If it is already sorted there will be no output but if it is not sorted single line information about disorder will be given.
$ sort -c fruits.txt
Sort Multiple Files
Sometimes data that will be sorted do not resides in a single file. Sort command do not have a native feature to use multiple files. Here other bash tools provides solution to merge multiple files into a single standard input stream. In this example we will
cat multiple files and redirect to the standard input of sort.
$ cat fruits.txt reverse_fruits.txt | sort
Sort By Given Column
Up to now we have sorted our data from starting first character of the line. Our data may be in a structured manner with columns and we may want to sort by using different columns others than first column. This can be done with other bash tools but sort provides a simple way to do this.
$ sort -k2 fruits.txt
Sort By Multiple Columns
Sort by column is easily implemented but what will happen if we want to sort according to multiple columns. Here
-k parameter can be used again with multiple values like below. We will sort primary with column 1 and then column 3
$ sort -k2,3 fruits.txt
Up to now sorting operation is done with alphabet and ASCII. Detecting numbers and sorting accordingly can be done with sort. For example in normal sort operation
1 is lower than
10 but if we want to sort according numeric values
-n parameter can be used like below.
$ sort -n -k2 fruits.txt
Sort By Size
While using Linux tools size information can be expressed in different way default expression method is prefixing with metric name like
128MB etc. Sort supports this type of expression which is very good for simple sort implementations.
$ ls -lh | sort -k 5 -h
Sort By Date
Unix and Linux months are expressed like
JUN etc. Sort can support this type of month expression and sorting them. The moths are listen column number 4 and we will sort according to column 4 as month value with the
$ sort -k4 -M fruits.txt