The sort command can be used in sorting a file and rearranging the records with the file in a certain order.
In this tutorial, we are going to help you explore some examples on the Linux sort command.
Before we start our tutorial, let’s take a look at the file that we are going to use in the next examples.
To display the content of a file in an ordered manner
To sort a file in reverse order
sort -r sort_examples
To sort a file contains numeric data inside
sort -n sort_examples
To sort a file containing numeric data but in reverse order
sort -nr sort_examples
To sort a file on a base of a certain column. Let’s assume you have a file called salary that looks like the below screenshot:
Now let’s sort the salary file based on the second column
sort -k 2n salary
To check if the given file has an ordered content or not
sort -c salary
To sort a file and remove the duplicates in a file
sort -u salary