The “sort” Command in Linux [16 Practical Examples]

The sort command in Linux is used to sort lines of text files. It is capable of sorting alphabetically and numerically, in ascending or descending order. It considers all contents as ASCII and rearranges them based on ASCII value. However, you can manipulate your output by providing a variety of options. In this article, I will show you the usage of the sort command in Linux with tons of examples.

A. Description

The sort command is common in Unix and Unix-like Operating systems. It is part of GNU Coreutils written by Mike Haertel and Paul Eggert. In ASCII every character has a numerical value. For example, the ASCII value of “A” is 65 and 97 for “a”. The sort command uses this value. So, if you want to sort “A” and “a”, “A” will always appear at the beginning. But you can make it case insensitive. You do numerical sorting, reverse sorting and all sort of things.

B. Syntax

The syntax of the sort command in Linux is simple. The syntax is sort, followed by one or multiple options and one or multiple files.

sort [OPTION]... [FILE]...

Note: In the above syntax OPTION and FILE  enclosed by square brackets and followed by 3 dots represents that multiple options and argument can be utilized at the same time.

C. Options

There are many options available for the sort command in Linux. You can check those yourself by going to the manual page. Type the following command-

man sort

Useful Options:

  • -f, –ignore-case (make sort case insensitive)
  • -M, –month-sort (sort by month)
  • -n, –numeric-sort (compare based on numerical value)
  • -R, –random-sort (shuffle the contents of the file)
  • -r, –reverse (reverse the comparison)
  • -c, –check ( check whether a file is sorted or not)
  • -k, –key( sort by key)
  • -o, –output (used to print output in another file)
  • -u, –unique (remove duplicates)

Note: The options in Linux CLI (Command Line Interface) are all case-sensitive, So be cautious while using them.

Configuring Your System

While following the examples below, some of you might run into this problem. You might see a different output than mine! I highly recommend you do one or two examples first, then come back here if you need to.

If you see different results, the most probable cause might be the localization settings. In short, “localization” refers to the language the operating system is using. Each character in a system is represented in a certain order and changing the locale settings might affect the output of the sort command in Linux.

 For a temporary solution, follow the steps below:

  1. At first open the Terminal
  2. Type the following command in the command prompt:
    export LC_ALL=C
  3. Now, press the ENTER button.Exporting value for the variable "LC_ALL"

Note: If you close the Terminal, you will return to the previous condition. You need to follow the steps above again.

For a permanent solution, follow the steps below:

  1. At first open the Ubuntu Terminal.
  2. Type the following command in the command prompt:
    nano ~/.bashrc
  3. Now, press the ENTER button.
  4. Add, the following line at the end
    export LC_ALL=C
  5. To save press CTRL + O and ENTER, to exit press CTRL + X.
  6. Now, type the following line and press ENTER.
    source ~/.bashrc

I have added two photos for you. The two commands below are for opening the .bashrc file and updating the environment.After opening the .bashrc file in the nano text editor, add an extra line like the one below, then update the environment.Adding an extra line in .bashrc file

Practical Examples of the “sort” Command in Linux

Here I will show you some examples of the sort command in Linux. As sort rearranges the contents of a file, I will use some files (letters, mix_letters, words, month, data.csv) throughout these examples. You can download those files from below:

Example 1: Sort a File with Lower Cases of Single Letters

At first, I am going to show how the sort command in Linux arranges multiple letters. I will work with the letters file here. Follow the steps below:

  1. At first open the Ubuntu Terminal.
  2. Type the following command in the command prompt:
    sort letters
  3. Now, press the ENTER button.

I have already told you that the sort command uses ASCII to sort content. The letter “a” is 97, whereas the letter “z” is 122 in ASCII. That’s why, as you can see, a is on the top.Before and after sorting a set of letters using the sort command in Linux


Similar Readings


Example 2: Sort a File with Mix Cases of Single Letters

Let’s see what happens when capital and small letters are mixed together. I will work with the mix_letters file here. Follow the steps below:

  1. At first open the Ubuntu Terminal.
  2. Type the following command in the command prompt:
    sort mix_letters
  3. Now, press the ENTER button.

In ASCII, values of letters “A”, “Z”, “a” and “z” are 65, 90,97 and 122 respectively. As a result, you will see capital letters at the top of small letters for the sort command in Linux.Sorting a set of characters with both upper case and lower case letters

Example 3: Case-Insensitive Sorting Using the “sort” Command in Linux

Now, what if you want to sort in a case-insensitive way? You can do that by doing the following. I will use the mix_letters file here:

  1. At first open the Ubuntu Terminal.
  2. Type the following command in the command prompt:
    sort -f mix_letters
  3. Now, press the ENTER button.

As you can capital letters and small letters are together now. But it is still following ASCII order; as you can see “A” came before “a”.Case insensitive sorting using the sort command in Linux

Example 4: Sort a File with Words

Now let’s sort some words. I will use the words file here. Follow the steps below:

  1. At first open the Ubuntu Terminal.
  2. Type the following command in the command prompt:
    sort words
  3. Now, press the ENTER button.

This one requires some explanation. Words (or sentences) are sorted based on the first letter. Now, what if the first two letters of two words are similar? Then sort will look for the second letter of the word and sort them accordingly. Here, “zebra” and “zoo” both have the letter “z” at the beginning. Now for the second letter, “e” comes before “o”. That’s why “zebra” is on top of “zoo”. Now, what if there are two exact words (or sentences) like “today” here? The sort command will just keep them together.Sorting a set of words using the sort command in Linux

Example 5: Numerical Sorting Using the “sort” Command in Linux

The sort command in Linux is capable of sorting numerically. You can try it by doing the steps below. I will use the numbers file here:

  1. At first, open the Ubuntu Terminal.
  2. Type the following command in the command prompt:
    sort -n numbers
  3. Now, press the ENTER button.

As you can see, at first, the numbers were disordered; now they are ordered.Sorting a set of numbers numerically

Example 6: Reverse Sorting Using the “sort” Command in Linux

Reverse sorting means turning the order upside down. Now the highest value will be at the top whereas the lowest value at the bottom. I think using the numbers file will be convenient here. You can do reverse-sorting by doing the following:

  1. At first, open the Ubuntu Terminal.
  2. Type the following command in the command prompt:
    sort -r numbers
  3. Now, press the ENTER button.

As you can the numbers are reversely sorted now.Reverse sorting using the sort command in Linux

Note: If you want to keep small letters on top of capital letters, reverse sorting is the way to go.

Example 7: Sort Multiple Files

Sorting multiple files is pretty simple. It is a lot similar to sorting a single file. You just need to put multiple files as arguments. I will use both words and numbers files here:

  1. At first, open the Ubuntu Terminal.
  2. Type the following command in the command prompt:
    sort words -n numbers
  3. Now, press the ENTER button.

The sort command will take the contents of all files as a whole and sort them.Sorting multiple files

Example 8: Sort a Column Using the sort Command in Linux

The sort command in Linux can sort column-wise. You all know about ls -l, right? If not, that’s okay. “ls -l” shows you information about files and directories, where the 4th column shows file size. Now I sort that column using the option “-k”. You can do that with me by following the steps below:

  1. At first, open the Ubuntu Terminal.
  2. Type the following command in the command prompt:
    ls -l | sort -nk2
    Note: The “2” after “k” denotes the column number. I also put the option “-n” as I am working with numbers.
  3. Now, press the ENTER button.

Now, after sorting, as you can see, the sizes are in ascending order.Sorting a particular column using the sort command in Linux


Similar Readings


Example 9: Sort and Remove Duplicates Using the sort Command in Linux

You may remember, in example 4, there were two similar words. You can remove duplicates by using the option “-u” (the “u” comes from the word “unique”). It can be called it unique sorting as well. I will use the words file here. Now follow the steps below:

  1. At first open the Ubuntu Terminal.
  2. Type the following command in the command prompt:sort -u words
  3. Now, press the ENTER button.

At first, see there are twotoday”. After unique sorting, as you can see, there is only onetoday”.Showing output for unique sorting

Example 10: Sort by Month Using the sort Command in Linux

The sort command in Linux has a feature to sort by month. However, it doesn’t care about days or months. As you have guessed, I am going to use the months file. Follow the steps below:

  1. At first open the Ubuntu Terminal.
  2. Type the following command in the command prompt:
    sort months
  3. Now press the ENTER button.

As you can see the months are ordered now.Sorting by months

Example 11: Sort and Save Output into Another File

You can always redirect your output of the sort command into a file. But the sort command in Linux comes with an extra feature as well. You save your output by following the steps below. Let’s use the numbers file here:

  1. At first open the Ubuntu Terminal.
  2. Type the following command in the command prompt:
    sort -o sorted_file numbers

    Note: Be careful. You need to type the name of the output file first, then the input file.

  3. Now, press the ENTER button.

I used the file sorted_file to store my output and the cat command to show you the contents of the file.Storing the output after sorting using the sort command in Linux

Example 12: Check if a File is Sorted or Not Using the sort command in Linux

Sometimes, mostly for scripting, you may require to check whether a file is sorted or not. You can do this simply by following the steps below:

  1. At first open the Ubuntu Terminal.
  2. Type the following command in the command prompt:
    sort -c sorted_file
  3. Now, press the ENTER button.

I used the file sorted_file from the previous example for this example. If your file is sorted, you won’t get noticeable output. But if not sorted, you will expect to see something like the second line here.Checking whether a file is sorted or not

Example 13: Random Sort Using the sort Command in Linux

Sometimes you may require to generate random output. The sort command in Linux can do that for you, even if the reputation of its name is at stake!! Let’s use the sorted_file from “Example 11”. Follow the steps below:

  1. At first open the Ubuntu Terminal.
  2. Type the following command in the command prompt:
    sort -R sorted_file
  3. Now, press the ENTER button.

My result may match yours. You will get different results every time.Showing output after sorting the contents a file randomly


Similar Readings


Example 14: Combining “sort” and “join” Command

The join is used to join lines of two files. To join sorted files, follow the steps below:

  1. At first open the Ubuntu Terminal.
  2. Type the following command in the command prompt:
    cat join <(sort words) <(sort -n numbers)

    Note: you are welcome to provide any option you like as long as they are inside the parentheses. And spacing matters.
  3. Now, press the ENTER button.

You will see the difference between “sorting multiple files” and “joining sorted files”. The joint command just joined the output of two different files, nothing else.Joining the output of two sorted files

Example 15: Sorting by a Delimiter

The default delimiter for the sort command in Linux is whitespace. However, you can work with other delimiters. For this example, I will use a CSV (data.csv) file and the delimiter of CSV file is the comma(,). Now I will use the option “-t”, and remember you need the provide your delimiter to it. Now follow the steps below:

  1. At first open the Ubuntu Terminal.
  2. Type the following command in the command prompt:
    sort -t, -k3 data.csv
  3. Now, press the ENTER button.

I will give a further explanation here. See, there is a comma (,) after the option “-t” because our delimiter is a comma. Now, I wrote “-k3” because I wanted to sort the 3rd column.Sorting by delimiter using the sort command in Linux

Example 16: Sort by Field

Now here is an interesting problem. If you want to sort the 2nd column with delimiter and column, you will see something like this. It is based on the “Mr.” and “Ms.” at the beginning. But I want to sort based on names. How will I do that?Output without for not sorting by fields

The answer is simple. Defining columns isn’t enough now, we need to define fields, which the sort command will use for comparison and sorting. Follow the steps below:

  1. At first open the Ubuntu Terminal.
  2. Type the following command in the command prompt:
    sort -t, -k2.3,2.5 data.csv
  3. Now, press the ENTER button.

You already know the purpose of “-t,”, right? Now next part, “-k2.3,2.5”. The “k” denotes the key (column), where 2 is the key (column) number. The 3 after the dot (.) denotes the starting field position and the 5 denotes the ending field position. So how our sort command is working here? First, it will go to the second column, ignore the first three fields (characters) and then use the next two fields (characters) for comparison.Showing output of sorting by fields

Conclusion

The sort command in Linux is one of the basic commands. It is a useful tool for all types of users. Practicing all the examples above will help you to have a farm idea on the sort command.


Similar Readings

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
Walid Al Asad

Hello Everyone! I am Walid Al Asad. Currently, I am working at a tech company named Softeko as a Linux Content Developer Executive. I live in Dhaka, Bangladesh. I have completed my BSc. in Mechanical Engineering from Bangladesh University of Engineering and Technology (BUET). You can find me on LinkedIn, and ResearchGate. Read Full Bio

Leave a Comment