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.
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:
Contents of file2.txt:
Now use the sort command to sort a file with the below syntax:
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.
sort -r inputfile.txt
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.
Consider the following file containing numbers:
sort -n file3.txt
You can also use -r to sort in descending order.
Advanced sorting options
Here are some specific sorting methods.
|sort according to general numerical value
|sort according to months
|sort human-readable numbers (e.g., 2K 1G)
|sort according to string numerical value
|shuffle, but group identical keys
|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
|ignore leading blanks (spaces or tabs)
|consider only blanks and alphanumeric characters
|fold lower case to upper case characters
|consider only printable characters
|get random bytes from FILE
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.