Signup/Sign In

Sort Command In Linux With Examples

Sorting data is a fundamental task when it comes to data manipulation, and thankfully, Linux has got us covered with its trusty sort command. Whether you're a programmer or a system administrator, this powerful tool will help you wrangle and process data with ease.

So how do we use it? It's as easy as typing “sort” followed by the name of the file you want to sort.

sort filename

By default, the sort command will sort the contents of the file in ascending order based on the first character of each line.

Examples of Sort Command On Linux

We will follow all upcoming examples and perform sort command on these files:

Contents of file1.txt:

aditya
reddy
anish
ayush
raichur
acharya
college

Contents of file2.txt:

ABC
all
Bag
bowl
DES

sort command

Now use the sort command to sort a file with the below syntax:

sort file2.txt

Sort command in linux

In the above screenshot, we can see that the sort command has output the content of file2.txt and sorted all lines in ascending order.

But wait, there's more! The sort command comes packed with a few features you might not know about.

Sort Commands Options in Linux

Let's explore some useful options/flags used to sort contents of a file in Linux.

1. Sorting in Reverse Order (-r)

You can use -r flag with sort command to sort in reverse order. By default, the sort command sorts alphabetically in ascending order.

The input file is the same as in the previous example.

Syntax:

sort -r inputfile.txt

sort command

2. Sorting numerically (-n)

The -n option is used to numerically sort a file. The -n option, like the preceding ones, is preset in Unix. This option is used to sort the file if it contains numeric data.

Example:

sort -n

Consider the following file containing numbers:

sort -n file3.txt

sort -n

You can also use -r to sort in descending order.

sort -nr

Advanced sorting options

Here are some specific sorting methods.

Option Description
-g, --general-numeric-sort sort according to general numerical value
-M, --month-sort sort according to months
-h, --human-numeric-sort sort human-readable numbers (e.g., 2K 1G)
-n, --numeric-sort sort according to string numerical value
-R, --random-sort shuffle, but group identical keys
-V, --version-sort natural sort of (version) numbers within text

The above table provides different options and their description.

Here are some more helpful flags for sort command

Option Description
-b, --ignore-leading-blanks ignore leading blanks (spaces or tabs)
-d, --dictionary-order consider only blanks and alphanumeric characters
-f, --ignore-case fold lower case to upper case characters
-i, --ignore-nonprinting consider only printable characters
--random-source=FILE get random bytes from FILE

FAQs

Let's take a quick look at a few questions which are most commonly asked.

Q. How to check if a file is already sorted?

You can use the -c option to check if a file is already sorted, without sorting it.

sort -c filename

This will output nothing if it is already in ascending order.

Q. How to sort multiple files with sort utility in Linux?

You can use the -m option to merge multiple sorted files into one output file, as shown below:

sort -m file1 file2 file3 ...

This will sort content of all specified files and output

Q. Can we specify Output File to sort command?

You can use -o option to specify the name of the output file you want to create, without having to use the redirect operator.

sort -o output.txt input.txt

The above command will sort “input.txt” file and write the sorted output to a file called “output.txt”.

Q. How to use sort command in Bash script?

You can definitely make use of sort command in shell scripts. For example, you can check if a file exists and use Bash if else statements to perform different sorting operations on the files.

Finally, The 'sort' command is a useful utility for anyone working with text files and can save time and effort in organizing and analyzing data.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.