Linux Sort Command Tutorial


Linux provides a lot of useful tools for system administrators. These tools give the administrator a developer power so the administrator can easily change, modify, filter, sort the data. The sort is another little but powerful tool. In this tutorial, we will look at different usage examples of the sort.

Example File

We will use following simple example file named fruits.txt

apple   1       good    JAN 
grape   5       bad     DEC 
banana  2       good    MAR 
melon   10      bad     JUN
Example File
Example File

sort Command Syntax

sort command will use following syntax.

sort [OPTION]... [FILE]... 
sort [OPTION]... --files0-from=F

sort Command Example

Sort can be used with file or standard input. In this example, we will sort file content according to each line start. As we see our file content is sorted alphabetically.

$ sort fruits.txt
Simply Sort
Simply Sort

Descending Sort

By default, the sort is done ascending. Sometimes it can be useful to sort reverse or descending mode. This can be done like below by providing -r parameter

$ sort -r fruits.txt
Descending Sort
Descending Sort

Add sort Command Output To File

After sort of operation, the new sorted content is printed to the standard output. One of the alternative ways to save output is writing to a file.

$ sort -r fruits.txt > reverse_fruits.txt
Add Output To File
Add Output To File

Ignore Case

The sort is by default case sensitive operation which means while sorting character case upper/lower is detected and sorted accordingly. If this does not suit you it can be disabled with -f or --ignore-case parameters.

$ sort -f fruits.txt
Ignore Case
Ignore Case

Check Specified File If Sorted

Another useful feature of the sort is to 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 the disorder will be given.

$ sort -c fruits.txt

Sort Multiple Files

Sometimes data that will be sorted do not reside in a single file. Sort command does not have a native feature to use multiple files. Here other bash tools provide a 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 Multiple Files
Sort Multiple Files

Sort By Given Column

Up to now, we have sorted our data from starting the 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 the 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 Column
Sort By Column

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
Sort By Multiple Columns
Sort By Multiple Columns

Sort According Numbers

Up to now sorting operation is done with the alphabet and ASCII. Detecting numbers and sorting accordingly can be done with sort. For example, in the normal sort operation 1 is lower than 10 but if we want to sort according to numeric values -n parameter can be used as below.

$ sort -n -k2 fruits.txt
Sort Numbers
Sort Numbers

Sort By Size

While using Linux tools size information can be expressed in different way default expression method is prefixing with a metric name like 1GB, 128MB etc. Sort supports this type of expression which is very good for simple sort implementations.

$ ls -lh | sort -k 5 -h
Sort By Size
Sort By Size

Sort By Date

Unix and Linux months are expressed like JAN , JUN etc. Sort can support this type of month’s expression and sorting them. The moths listed to column number 4 and we will sort according to column 4 as month value with the -M parameter.

$ sort -k4 -M fruits.txt
Sort By Date
Sort By Date

LEARN MORE  Linux uniq Command Tutorial With Examples

1 thought on “Linux Sort Command Tutorial”

  1. Pingback: URL

Leave a Comment