Linux Commands

How to Use the Bash Sort Command

“The Bash sort command is used to sort a file’s content and give output in the stdout. With sort, you can sort a file’s contents alphabetically, in reverse, numerically, ascending, or descending order. The sort command sorts a file’s contents line by line, and the input you give acts as the sort key. By the end of this guide, you will have an understanding of using the Bash sort command.”

Sorting Using the Sort Command

The sort command supports various options, and you can view them on its help page. We will discuss the common options using examples.

If you run the sort command followed by a file, it will sort the contents alphabetically and display the sorted output.

Sorting Uppercase and Lowercase

In the above example, we only have lowercase letters. What if we had a mixed case? Let’s create a file with mixed content and see how sort will execute.

Here’s our file.

Let’s sort it out and see what we get.

We can note that the lowercase letters were arranged before the uppercase letters.

Sorting Numbers

When you want to sort numbers, you need to add the -n flag, and sort will rearrange the numbers in ascending order.

Here’s an example.

Sorting in Reverse

Using the -r flag, you can instruct sort to reverse the arrangement.

For instance, let’s sort the file containing numeric values and see how it arranges them.

Also, you can reverse alphabets to start from z/Z, as in the case below.

Sorting Multiple Files

You can combine the multiple files you want to sort simultaneously with sort. In the example below, we have two files.

To sort them at once, we need to add their filenames separated by a space and sort them at once, but the output will get mixed up.

Removing Duplicates

If you have duplicate values in a file and don’t want to print them when sorting the values, you can use the -u flag to remove them.

Take a look at the file below having duplicate lines.

If we run the sort command with the -u flag, we can notice that we didn’t print the duplicate values.

This is helpful when you want to create a unique arrangement of files containing numbers or names.

Combining Sort Options

Sometimes, you may need to use different options at once. For instance, sorting numeric values in reverse. That, too, is possible. Let’s have an example of reversing numeric files using the syntax below.

$ sort -nr FILENAME

Sorting Table Columns

With the -k option, you can sort a table based on available columns.

For this example, let’s create a table with three columns.

We can then sort the second column, which contains numbers using the -n option and the -k to specify column 2. Use the command below.

$ sort -k 2n table.txt

The above example shows a duplicate in the specified second column. If you wanted to remove the duplicates, you could achieve that by adding the -u flag as in the image below.

Check if File is Sorted

Before sorting a file, you can verify if the file is already sorted. For that, use the -c flag. If the file is sorted, you won’t get any output.

However, if the file is not sorted, you will get a message stating the file has a disorder.

Sorting Months

If you had a file containing months but not in a sorted format, you could sort the file using the -M flag.

Saving to File

If you want to create a new file containing the sorted content instead of displaying it on the standard output, use the -o flag followed by the file’s name.

In the example below, we are saving the sorted months in a new file.

Conclusion

The Bash sort command makes the sorting of contents of a file simpler. You can sort a file’s contents in various formats. Using this guide, you have various options that you can try and sort your file. That’s it, folks. Hopefully, this article enlightened you on how to use the sort command.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.