Linux Sort Command With Examples


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.

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

Syntax

sort command will use following syntax.

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

Simply Sort

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
Simply Sort
Simply Sort

Descending Sort

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 -r parameter

$ sort -r fruits.txt
Descending Sort
Descending Sort

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
Add Output To File
Add Output To File

Ignore Case

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 -f or --ignore-case parameters.

$ sort -f fruits.txt
Ignore Case
Ignore Case

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
Check Input File If Sorted
Check Input File If Sorted

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 Multiple Files
Sort Multiple Files

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 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 Numbers

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 Numbers
Sort Numbers

Sort By Size

While using Linux tools size information can be expressed in different way default expression method is prefixing with 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 expression and sorting them. The moths are listen 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  chage Command Tutorial With Examples To Manage User Accounts In Linux

1 thought on “Linux Sort Command With Examples”

  1. Pingback: URL

Leave a Comment