sort Command in Linux with Examples

In Linux, manually sorting the file’s content is time taking, especially when you have to deal with large size of content files. The sort is the built-in utility of Linux for sorting the content of the file line by line. It provides the facility to sort content with different options. In this article, we will demonstrate the sort command in Linux. The content for the post is:

Let’s start with the basic introduction of the sort command.

What is the sort Command in Linux?

As mentioned above, sort is the command to sort to arrange the content in a particular order; by default, it sorts the file considering the content is in ASCII (encoding format). The syntax for the sort command is given below:

Syntax

$ sort [Options] [FIle_Name]

Type the “sort” keyword along with the desired options and write the file name to be sorted.

To check the various options that can be used with the sort command, run the help command in the terminal:

$ sort --help

All possible options, along with the command’s syntax, are displayed.

Let’s use some of the options given in the image.

How is the Sort Command Used in Linux?

To use the sort command in Linux, there are various options. The few most important ones are executed in the below examples. Consider the following file output in mind while understanding the sorting of examples 1 and 2.

$ cat itslinuxfoss.txt

Example 1: Sorting in Alphabetical Order

To sort the content in alphabetical order, simply type the sort command with the file name:

$ sort itslinuxfoss.txt

The content has been displayed in alphabetical order.

Note: The original file content will not be changed using the sort command.

Example 2: Sorting in Reverse Order

For sorting the content in a reverse manner, either numerically or alphabetically, put the “-r” flag with the sort command:

$ sort -r itslinuxfoss.txt

The content is printed in a reverse manner.

Example 3: Sorting Through Numerical Values

Similarly, to sort content by numeric values, let’s add the number at the start of the names:

$ cat itslinuxfoss.txt

Now, sort it using the given command.

$ sort itslinuxfoss.txt

The content is sorted by numerical values.

But wait, the other concept is still remaining, let’s add higher numbers instead of 1, 2, 3:

$ cat itslinuxfoss.txt

Let’s sort it out:

$ sort itslinuxfoss.txt

Just look at the output, most of the users think that sort connoisseurs the whole number while sorting such as 100, 101, 10, 150, etc. Still, in actuality, it only considers the starting values such as 1 from 100, 2 from 21, and 5 from 51. In simple words, the numeric values are sorted out by their leading numbers.

Example 4: Sorting Randomly

To sort the content randomly, use the “-R” option with the sort command. Consider the following output of the file:

$ cat itslinuxfoss.txt

Randomly sorting of the file content is obtained as follows:

$ sort -R itslinuxfoss.txt

The content is sorted randomly.

Example 5: Sorting Upper and Lower Case

While sorting the content, the small letters would be preferred first by the sort command as compared to capital letters. To sort the upper and lower case, let’s change some words into small letters:

$ cat itslinuxfoss.txt

After that, let’s sort this content

$ sort itslinuxfoss.txt


As can be seen, small letters are preferred first, and content is sorted out accordingly.

Example 6: Sorting File with Redirection Operator

To sort the content with redirection operators will forward the output to the next command, and in the next command, we are creating the “output.txt” file. It will store the sporting content of the file:

$ sort itslinuxfoss.txt > output.txt

Let’s check the output of the created file:

$ cat output.txt

The content is sorted out in the “output.txt” file.

Example 7: Sorting Particular Column

The sort can also be applied to the specific column of the content. We have 2 columns in the file, one column has names, and the second column has numbers:

$ cat itslinuxfoss.txt

Let’s say we want to sort according to column 2, so we will use the “K” flag and will give the column number:

$ sort -k2 itslinuxfoss.txt

The content is sorted according to column 2 which is the same concept that we have discussed in example 3 (numerical values sorting).

Example 8: Ignoring Case Sensitivity While Sorting

To ignore the case sensitivity of the characters, we can use the “-f” flag in the command. Let’s say we have the following output of the file in which both upper and lower case are mixed:

$ cat itslinuxfoss.txt

Let’s sort it without the case sensitivity:

$ sort -f itslinuxfoss.txt

The content is sorted out without case sensitivity.

Example 9: Sorting by Month

To sort the months of the year, the sort command provides the option of “-M”, we have the file content in which a few months or years are written without sequence:

$ cat itslinuxfoss.txt

Let’s sort this using the given command:

$ sort -M itslinuxfoss.txt

The months are sorted out.

That’s how the sort command works in Linux.

Conclusion

In Linux, the sort is a built-in utility for arranging the content of the files. Various options can be considered while sorting any content. This post has briefly illustrated the usages of the sort command with help of examples.