What is sort command in linux?

“Sort” command is used to arrange the lines within a file in an alphabetical order or in a reverse alphabetical order or can be used to sort the files within directories. It is mostly used along with pipes to sort the output of files along with many options. In the following post, you will learn what is sort command in linux and how to use it.

By default, “sort” command arranges the lines based on case sensitive.

For example, the below figure consists list of animals with file names animals.txt

After using sort commands, the list of animals are sorted with case sensitive.

You can sort the lines based on the fields which are separated by delimiters like colon(:), semi-colon(;), comma(,), or whitespace

The following figure has list of users delimited by colon(:)

By using below mentioned command, sorted the third filed lines based on the numeric order.

sort –t: -n –k3 users.txt

-t: – To denote the delimiter(:) in the lines, you can mention any delimiter along with ‘t’ for sorting.

-n – To sort the lines based on numbers(sort type).

-k3 – To sort the 3rd field of every lines.

For reverse sorting, you can use option r in the below command.
sort –t: -n –r –k3 users.txt

How do you feel about this post? Drop your comments below..