How to Use the sort Command in Linux

This tutorial explains how to use the sort command in Linux through practical examples. Learn how to sort multiple files or content of a single file based on several criteria.

The sort command is used to sort the content of a file. By default, this command serializes the content alphabetically. For sorting, it uses the first letter of each line. To sort contents based on other criteria or sort contents in reverse order, we have to use related options with this command.

In this tutorial, we will not only discuss these options in detail but also understand how to use the sort command in sorting the files of a directory.

Setup

To understand how the sort command works, you do not need any special lab setup or a privileged user account. To perform all exercises/examples explained in this tutorial, you can use any regular user account.

Login from any regular user account and create a sample file with some dummy content as shown in the following image.

sample file for the practice of sort command

This file contains a few user records. Each record has been stored in a separate row and contains only two columns; name and age. Records have been stored without following any order or sequence.

Sorting a file in alphabetical order

Advertisements

To sort a file in alphabetical order, we use the sort command without any option. As mentioned above, if this command is used 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

The output of this command is displayed in the following image.

default sorting of the sort command

Sorting output based on a column

By default, the sort command 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 the age of users in the second column of each record. To sort this file based on the age column, we can use the following command.

$sort -k2 test_file

The following image shows this command with the output.

sort command k option

Column base sorting works effectively only if a similar type of data is stored in the same column of each line. For example, in our test file, we stored the user's age in the second column of each line.

Sorting records in reverse order

By default, the sort command arranges texts and numbers in the "A to Z" and "Smallest to Largest" orders respectively. To sort in reverse order, use the -r option with the sort command.

You can also combine this option 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

If we use the -r option as shown below, then this command will sort the output in reverse direction.

$sort -r -k2 test_file

The following image shows this command with the output.

sorting in reverse

Viewing and sorting multiple files

If you have multiple files with similar data and want to view and sort their data, you can use the -m option with the sort command.

In our test file, we stored a few users' names and ages. Suppose there is one more file and that also contains similar information.

The following image shows both files with output.

sample files for 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 is only used to view the data of multiple files. It does not force the sort command to display the output in any particular order. To view the output in any specific order, use the related options along with the -m option.

For example, the following command displays the data of two files named file1 and file2 after sorting their data based on the second column.

$sort -m -k2 file1 file2

Saving the sorted output in a file

By default, the sort command displays output on the terminal. To save the output in a file, we have two choices; 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 printer or to a file. To redirect a command's output, the following syntax is used.

$first command > device or file name

If a file name is used, the output of the left command is saved in that file.

If you don't want to use the shell redirection feature, you can the -o option with the sort command. The -o option instructs the sort command to save the output in a file. To use the -o option, use the following syntax.

$sort [options] file -o file-name

Let's understand both options with example.

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

The following image shows this command with output.

redirect the output of sort command

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

$sort -r demo_file -o new_file1

The following image shows this command with the output.

saving output of the sort command

Sorting numbers

By default, the sort command does not use the whole word or number in sorting. It performs sorting based on the individual characters or digits 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 this file. Make sure the numbers are various in digits. Now sort this file with the sort command.

The following image shows this exercise with output.

sorting a number file

As you can see in the above image, numbers have been sorted based on the individual digits, not on the value of the whole number.

To sort numbers based on their values, the -n option is used with this command. The following command sorts the file created above using the values of whole numbers.

$sort -n number_file

The following image shows this command with output.

sort command n option

Sorting files based on their sizes

If you want to sort the files based on their sizes, you 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, the current directory is used. By default, the ls command displays only the name of files and directories. To view the detailed information, the -l option is used with this command.

The following image shows an example of this command.

ls command

As you can see in the above image, the ls -l command displays size in the fifth column. If we redirect this output to the sort command as the input and instructs the sort command to sort the contents based on the fifth column, we will get a list of all files which are arranged based on their sizes.

To redirect a command's output to another command as the input, the pipe (|) sign is used. The following command redirects the output of the ls -l command to the sort command.

$sort ls -l | sort -k5

The following image shows this command with the output.

sorting output of ls command

That's all for this tutorial. If you like this tutorial, please don't forget to share it with friends through your favorite social platform.

Advertisements

ComputerNetworkingNotes Linux Tutorials How to Use the sort Command in Linux