How to Use the sort Command in Linux

The sort command sorts a file's contents. By default, it uses the first letter of each line for sorting. Without any option, it sorts contents in ascending order. It supports many options that allow us to perform sorting based on other factors and orders. This tutorial explains how to use this command to perform sorting based on various things.

The sort command is a shell built-in command. It does not require special privileges to run. We can use it under any account.

Creating a test file

Login from a regular user account and create a text file. Add a few lines containing a name and number to it. Use a comma to separate fields.

sorting a file in alphabetical order

To sort a file alphabetically, use the sort command without any option. As mentioned above, without any option, it sorts content alphabetically based on the first letter of each line. For example, the following command arranges the contents of our test file alphabetically and prints them on the terminal.

$sort test_file

default sorting

Sorting output based on a column

By default, it uses the first letter of each line to arrange the output. To use any other word/column, specify its sequence number with the option -k. For example, in our test file, we stored numbers in the second column of each record. The following command sorts it based on these numbers.

$sort -k2 test_file

sort command with the k option

Column-based sorting works only when we save a similar data type in the same column for each line. For example, in our test file, we stored numbers in the second column of each line.

Sorting records in reverse order

By default, it arranges texts and numbers in the A to Z and Smallest to Largest orders. We use the -r option with the sort command to sort in reverse order. We can combine it with other options to get more specific results. For example, the following command sorts our test file based on the value stored in the second column.

$sort -k2 test_file

The following command sorts it in reverse order.

$sort -r -k2 test_file

sorting in reverse

Sorting multiple files

The -m option combines the data of all specified files and passes it to the sort command. Let us suppose we have one more file that contains identical data.

sample files for the sort command

If we want to view the data of both files at the same time, we can use the -m option. The following image shows an example of this option.

sort command m option

The -m option only combines the data of all given files. It does not force the sort command to display the output in any particular order. We need to use the related options to view the output in any specific order. For example, the following command combines the data of file1 and file2, sorts it based on the second column, and displays the result on the terminal.

$sort -m -k2 file1 file2

Saving the sorted output in a file

By default, it displays output on the terminal. We have two choices to save the output in a file: the shell redirection and the -o option. Linux shell allows us to redirect the output of any command to any non-standard display device, such as a printer or a file. Use the following syntax to redirect a command's output.

$first command > device or file name

Alternatively, you can use the -o option. It instructs the sort command to save the output in a file. To use it, use the following syntax.

$sort [options] file -o file-name

Let's understand both options through examples. The following image shows an example file named demo_file.

default sorting

The following command sorts the content of this file in reverse order and saves the output in a new file named new_file.

$sort -r demo_file > new_file

redirecting the output of the sort command

The following command also does the same job but uses the -o option instead of the shell redirection feature.

$sort -r demo_file -o new_file1

saving the output of the sort command

Sorting numbers

By default, the sort command does not use the whole word or number for sorting. It uses only the first character or digit of the word or number stored in the specified column. Let's take an example.

Create a new file named number_file and store some dummy numbers in it. Make sure the numbers are various in digits. Sort this file using the sort command.

sorting a file containing numbers

As the above output shows, it sorted numbers based on the individual digits, not whole numbers. The -n option allows us to sort numbers based on their values.

$sort -n number_file

sorting based on whole numbers

Sorting files based on their sizes

If we want to sort the files based on their sizes, we can use the sort command with the ls command. The ls command displays the files and directories of the specified directory. If a directory name is not specified, it uses the current directory. By default, the ls command displays only the names of files and directories. The -l option is used with this command to view the detailed information.

the ls command

The ls -l command displays size in the fifth column. If we redirect the output of this command to the sort command as the input and instruct the sort command to sort the contents based on the fifth column, we will get a list of all files arranged based on their sizes. We use the pipe (|) sign to redirect a command's output to another command as the input. The following command redirects the output of the ls -l command to the sort command.

$sort ls -l | sort -k5

sorting the output of the ls command

Conclusion

The sort command is a versatile tool for organizing data within text files based on various criteria. It allows users to sort lines alphabetically, numerically, and even by specific columns while offering the ability to reverse the sorting order and combine multiple files. Additionally, it provides options for saving the sorted output directly to a file, ensuring that data management is efficient and straightforward.

ComputerNetworkingNotes Linux Tutorials How to Use the sort Command in Linux

We do not accept any kind of Guest Post. Except Guest post submission, for any other query (such as adverting opportunity, product advertisement, feedback, suggestion, error reporting and technical issue) or simply just say to hello mail us ComputerNetworkingNotes@gmail.com