Linux Commands

Sort Command in Linux with Examples

To organize the data in a precise sequence or sort the file, use the sort command. The file’s data is sorted line by line using the sort command. If a record is in alphabetical order, the file is sorted alphabetically. Otherwise, it is sorted in ascending order if the record contains numeric information. Linux’s sorting feature offers a variety of flags from which we can choose to sort in reverse or by column, etc. We will utilize a few of its flags in this article.

Using the Sort Command in Examples

We can sort the lines in text files with sort. The sort function can be utilized to sort the data files. The output of such sorting is printed on standard output. The contents of a file can be rearranged sequentially or alphabetically, and the data can be placed in increasing or decreasing order to make it easier to read. Now, using various examples of the sort command, we first show the names of the files that are currently on our desktop in the terminal window. However, to do this, we must first get the directory for our desktop using the “cd” command, then type “Desktop”, followed by a space and then a slash (“/”) at the end.

Linux@linux:~$ cd Desktop/

As a result, when we enter this command, the desktop directory is presented. Next, we run the “ls” command to have the list of files currently on the desktop which are displayed on the terminal.

Linux@linux:~/Desktop$ ls

The following image shows how all of the desktop files are displayed on the screen after running this command. There are three files that have the “.txt” extension since they are all text files with the “file.txt”, “name.txt”, and “record.txt” names.

Now, let’s open any file on the terminal. In this case, we open the “name.txt” file. To open this file on the terminal, we use the “cat” command. “Cat” is used to extract an information from the files and output its contents. It makes it easier to create, read, and concatenate the files. The file’s name, “name.txt”, is then written after a space is entered.

Linux@linux:~/Desktop$ cat name.txt

As can be seen in the following image, when we run this command, it opens the “name.txt” file in the terminal windows. There are eight lines in this file that include a list of names. “Jhon”, “Smith”, “Alex”, “Jungkook”, “Dave”, “Seth”, “Wade”, and “Jorge” are on the list.

You can see in the following snippet that when we run the aforementioned command, it displays a correct result and the file’s data is the same. You can also view this file directly from the desktop.

Now that we have the command, we sort this file by typing “sort”, followed by a space, and then the file’s name which is “name.txt”. By using this command, the data in the file is sorted alphabetically.

Linux@linux:~/Desktop$ sort name.txt

When we run this command, you can see in the following image that the file’s displayed data is now sorted. Previously, the first element in the file was “Jhon” which starts with the letter “j”. The second element was “Alex” which starts with the letter “A” which is the first alphabet. Now, we can see that it displays “Alex” first. After that, it displays “Dave”, and then “Jhon”. Similarly, you can see that the data is now sorted.

Multiple File Sorting

We sort multiple files in this section. We use two files in this instance – “Name.txt” and “Record.txt” are the files that we utilize. To accomplish this, we employ the command in which we first put “sort”. Then, we put a space as well as the names of the files which are “name.txt” and “record.txt”.

Linux@linux:~/Desktop$ sort name.txt record.txt

This command displays both the data from the files and the output in the terminal when we run it by typing it in. We have a number just like in the record file. These numbers are sorted in a sequence or you could say, in ascending order. The numbers are “20%”, “40%”, “80%”, and “90%” after sorting. Additionally, the names in the second file, “name.txt”, are similarly arranged alphabetically. The list that results from sorting includes “Alex”, “Dave”, “Jhon”, “Jorge”, “Jungkook”, “Seth”, “Smith”, and “Wade”.

Reversing the Order of a Sort Using –R

In this section, we use one of Linux’s sorting flags, “-r”, which is used to sort the file in reverse order. Sorting in Linux also offers a variety of different flags to do different actions. So, the first thing that we’re going to do is open a file that we need to reverse on the terminal. In this case, we use the “record.txt” file. To open this file on the terminal, use the “cat” command. Then, type the file’s name, “record.txt”.

Linux@linux:~/Desktop$ cat record.txt

When we execute this command, the screen shows the record.txt file.

Now, we sort this file in reverse order by entering the command in which we first type “sort”. Then, a space followed by the reverse order command, “-r”. Finally, the “record.txt” file name .

Linux@linux:~/Desktop$ sort –r record.txt

Following the execution of this command, you can see in the following screenshot that the file’s contents are sorted in reverse or we could say that it is shown in descending order with “90%” to appear first, followed by “80%”, “40%”, and then “20%”.

Sorting the Files and Transferring the Output to a Different File

The “name.txt” file is now sorted in this section. Its output is transferred to another file. To accomplish this, we use the command in which we first type “sort”, then thename.txt file name, followed by the symbol “>” and the name of the file to which we want to transfer the output, which in this case is “output.txt”.

Linux@linux:~/Desktop$ sort name.txt >output.txt

Now, we to use the “ls” command to check whether or not the “output.txt” file is generated and whether the “name.txt” file transfer result is successful.

Linux@linux:~/Desktop$ ls

As you can see in the following image, this program creates a new file named output.txt and places it on the desktop.

Now, in the subsequent step, we use the command to open this file. To do this, we type “cat” and the file’s name, “output.txt”, then hit enter. When this command is executed, the file’s data is displayed on the screen. As you can see, it is now sorted alphabetically.

Sorting by Column

Now that we decided to sort the file by columns, let’s open the file that we want to sort. This file has the name “file.txt” on the desktop and it has two columns. The first column contains the names of various programming courses while the second column contains the course’s code. Let’s open the file first using the “cat” command. Then, type the file’s name, “file.txt”, so that when we run this command, the file will open on the terminal, as can be seen in the following image:

Now, that we sorted this file, we sort the second column. To do this, we use the command in which we first type “sort” and then “-k2”. We use “-k2” because we sort the column 2. Finally, we type “file.txt”.

Linux@linux:~/Desktop$ sort –k2 file.txt

Now that the file is displayed on the terminal when this command is performed, you can see in the following image that column two is sorted while column one is not. You can also sort the column one using the “k1” keyword in the command. As you can see, “python 110” is displayed first, followed by “C# 123”, “Java 340”, “C++ 400”, and “Html 444”. Additionally, we can state that the courses in column “2” are organized in ascending order.

Conclusion

With many examples, we covered the sort command in Linux in this article. In the first section, we use the “ls” command to list all of the desktop’s files before sorting is performed. We used “sort” to arrange the files that have records alphabetically or those files are arranged alphabetically after sorting. We also reviewed how to sort multiple files. The sort command’s flags were covered in the second section where we talked about sorting in reverse order, moving the results to another file after sorting, and sorting by column.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.