Linux provides you with several utilities that you can use to process text files. Whether you want to remove duplicate data or sort the content inside a file, Linux command-line tools have everything you need.

This article will demonstrate the sort command and how you can use it to sort the content inside a text file and arrange it accordingly.

What Is the sort Command?

As mentioned above, the sort command helps a user in arranging the content of a text file in a particular order. Several options are available that allow you to sort the file according to your wish. It is a standard Linux program that can sort a text file alphabetically, numerically, by column, and more, in either normal or reverse order.

Other functionalities of the command include ignoring character cases while sorting, sorting a file by month, ignoring blanks in a file, and random sorting. Using sort, you can also check whether a file is already sorted or not.

How to Use sort in Linux

Although sort contains several methods and flags that you can use, it is remains easy to learn.

Basic Syntax

The basic syntax of using sort is:

        sort filename
    

...where filename is the absolute or relative path of the text file that you want to sort.

By default, sort will arrange the content as per the following criteria:

  1. Lines beginning with numeric characters have the highest priority.
  2. The command will sort the lines alphabetically, after sorting the lines beginning with numbers.
  3. Lines starting with lowercase characters precede the lines starting with the same character in uppercase.

Consider a text file named textfile.txt containing the following information:

viewing the text file linux

To sort the file using default configuration:

        sort textfile.txt
    

Output:

sorting textfile using sort

Create a New Output File

The sort command doesn't modify the content of the file. It simply sends the sorted content to the standard output. However, this doesn't mean that sort can't create a new file. You can use the -o flag to specify the name of the sorted file and sort will automatically create the file for you and add the content.

        sort -o sortedfile filename
    

...where sortedfile is the name of the output file and filename is the original file that needs sorting.

To sort textfile.txt and create a new output file for the content:

        sort -o sorted.txt textfile.txt
    

Output:

sending output to a new file

Sort Multiple Files

To sort more than one file at once, simply pass the filenames separated with the Space character.

        sort textfile.txt textfile2.txt
    

Output:

sorting multiple files using sort

Note that sort will merge the output of the files and display them together in the terminal.

Reverse Sort a File

If you want to reverse the arrangement of the content, use the -r flag with the default command. The -r in the following command stands for Reverse.

        sort -r textfile.txt
    

Output:

sorting files in reverse order

Sort a File Numerically

To sort a file containing numeric data, use the -n flag with the command. By default, sort will arrange the data in ascending order.

        sort -n numbers.txt
    

Output:

sorting file with numbers in linux

If you want to sort in descending order, reverse the arrangement using the -r option along with the -n flag in the command.

        sort -rn numbers.txt
    

Output:

reverse sorting a file with numbers

Ignore Character Case While Sorting

By default, sort takes into account the character case of the content. Lines starting with lowercase characters precede lines starting with the uppercase version of the same character. For example, "he is a boy" will precede "He is a boy".

If you want sort to ignore the character case, specify the -f or the --ignore-case flag as follows:

        sort -f textfile.txt
sort --ignore-case textfile.txt

Output:

sorting files in Linux

Sort a File Based on Month

Using the -M flag, you can modify the order of a file's content based on month names.

        sort -M textfile2.txt
    

Output:

sort files based on month names

Ignore Leading Blanks

Sometimes, the file that you want to sort might contain spaces or tabs. To ignore such blank characters, use the -b flag.

        sort -b fileblanks.txt
    

Output:

ignore blanks while sorting in linux

Sort a File According to a Column

If you've got a text file with data arranged in separate columns, you can sort the file according to the content of a column. All you need to do is pass the column number along with the -k flag.

Consider a text file containing file information with different columns. To sort a file named output.txt according to the eighth column:

        sort -k8 -rn output.txt
    

Output:

sort according to column values

Pipe Sort With Other Commands

You can even use sort with other Linux commands to modify the arrangement of the output. For example, to sort the output of the ls command according to the size of the files:

        ls -la | sort -k5 -rn
    

Output:

piping sort with other commands

Sort a File Randomly

You can use the -R flag if you want to randomize the order of the lines in a text file. Consider the file textfile.txt:

viewing textfile using cat
        sort -R textfile.txt
    

Output:

sorting files randomly

Sort Version Numbers in a File

If you have got a text file containing version information associated with a package, you can sort its content using the -V or --version-sort flag.

        sort -V version.txt
sort --version-sort version.txt

Output:

sorting version data with sort

Check if a File Is Sorted

The -c flag will help you in identifying files that are already sorted according to the options specified. If the content of the file is properly sorted, sort will not display any output.

To check whether the file textfile.txt is sorted:

        sort -c textfile.txt
    

Now, let's sort the file and save its output to a new file named sorted.txt. On issuing the following command:

        sort -c sorted.txt
    

Output:

check files with sort in linux

You can also specify various flags to check the arrangement of the file according to particular criteria. For example, to check if the file numbers.txt is sorted in descending order:

        sort -c -rn numbers.txt
    

You will see an output stating that the file is not sorted properly. Let's sort the file and check if the new file passes the test.

        sort -o sorted.txt -rn numbers.txt
sort -c -rn sorted.txt

Output:

check a file with numbers

Sort the File and Remove Duplicates

The file that you are working with might contain duplicate data. Although you can use the uniq command to remove such information from the file, sort can perform this task for you. The -u or --unique flag is what you need.

Consider a file named duplicate.txt:

file with repetitive data

To sort the file and remove the repeated data:

        sort -u duplicate.txt
    

Output:

remove duplicate data from the output

You can see that when you use the -u flag, sort only displays distinct lines and arranges them according to the criteria specified.

Working With Text Files in Linux

Although the power of command-line text editors is unmatchable, you can still opt for a graphical editor such as gedit to modify the content of a text file easily. Also, it's a better choice for those who are new to Linux and can't deal with the terminal.

The best way to start with the command line, and Linux in general, is by practicing basic commands first. After covering the fundamental utilities, gradually advancing towards more complex commands is probably the best approach.