Linux Sort Command: A Comprehensive Guide with 12 Practical Examples

Try this guide with our instant dedicated server for as low as 40 Euros

sort command in linux

For the novice and seasoned system administrators, the sort command is a notably powerful and versatile tool. 

This command offers an efficient way to organize file content in the extensive Linux ecosystem. It is a versatile command that you can apply in diverse scenarios such as sorting lines in a file, extracting duplicate entries, or resorting data in reverse order.

In this comprehensive guide, we will discuss how to use the sort command to streamline your workflow. We will also highlight the applications with 11 practical examples that demonstrate the utility’s flexibility.

Let’s start with an overview of the sort command.

Table Of Contents

  1. What is the sort Command?
  2. The Linux Sort Syntax
  3. Linux Sort Options
  4. 12 Linux Sort Examples
    1. The Prerequisites
    2. Example #1: Sort Text Alphabetically
    3. Example #2: Numerical Sorting
    4. Example #3: Reverse Sorting
    5. Example #4: Sort by Unique Values
    6. Example #5: Sort by Specific Column
    7. Example #6: Sort by Month
    8. Example #7: Sort the Human-Readable Units
    9. Example #8: Ignoring Case While Sorting
    10. Example #9: Sort the Contents and Output to Another File
    11. Example #10: Check If a File Is Already Sorted
    12. Example #11: Sort with a Custom Field Separator
    13. Example #12: Merge Two Sorted Files
  5. Conclusion
  6. FAQs

What is the sort Command?

Many users consider sort as a basic tool for text manipulation. However, a deeper dive into the capabilities of the tool shows that it’s a great option for optimizing file content management. 

Used in Unix and many Unix-like operating systems, the sort command allows users to sort file contents through various criteria, including alphabetically, numerically, and even by month name, directly from the standard input or within an input file.

In general, the sort command is useful in the following scenarios:

  • When you need a standard command-line program that prints the lines of its input. 
  • When you need to rearrange the contents of a text file, line by line.
  • Sort alphabetically (A to Z), in reverse order, by number, and by month.
  • When you need to consider all contents as ASCII and rearrange them based on ASCII value.

The Linux Sort Syntax

The Linux sort command sorts the contents of the file in a specific order based on the first character of each line. The Linux sort command syntax is as follows:

# sort [options] filename

Here,

Options are flags that modify the default sorting behavior.

filename is a text file you want to sort. 

Note: If you run the sort command without specifying any options, it displays the file’s contents sorted according to the default rules set by the current locale. 

The rules of sorting are:

  • Lines that begin with a number are placed before those that start with a letter. These are arranged in ascending numerical order, from 1 to 10.
  • Lines that begin with letters are sorted alphabetically in ascending order, from A to Z.

Linux Sort Options

The sort command in Linux comes with a variety of options that allow you to customize the sorting process according to your needs. Here are some of the most commonly used options:

Linux Sort Options

These options can be combined to achieve complex sorting behaviors.

For example, you can sort a file numerically in reverse order while ignoring case, or sort based on a specific key while removing duplicate entries. 

This versatility of the sort command makes it a powerful tool in text processing and data manipulation in Linux environments.

12 Linux Sort Examples

Now that you have a good idea of the standard syntax of the sort command, let’s see the command in action. But first, let’s take a look at the prerequisites.

The Prerequisites 

Before moving in, ensure you have the following:

  • A system running a mainstream Linux distribution
  • A user account with sudo privileges

Example #1: Sort Text Alphabetically

Run the following command to sort the lines of a text file alphabetically:

# sort filename.txt

Consider the following screenshot that shows the original order of the names in the files.

sort filename.txt

Now, run the following command: 

# sort filename.txt

The command sorts the lines of filename.txt in alphabetical order and displays the sorted output in the terminal.

sort filename.txt 2

Example #2: Numerical Sorting

If you have numbers in your file and wish to sort them numerically, run the following command:

# sort -n numbers.txt

Consider the following contents of the files that show the original lineup of numbers in the file.

sort -n numbers.txt

To sort these numbers, run the following command:

 # sort -n numbers.txt

sort -n numbers.txt 2

The command sorts the lines of numbers.txt numerically and displays the sorted output in the terminal.

Example #3: Reverse Sorting

You can sort the contents of a file in reverse order (whether alphabetically or numerically) by adding the -r flag to the sort command in the following syntax:

# sort -r filename.txt

As you can see, the contents of the file presents the names in the original order. 

sort -r filename.txt

Now, run the following command to reverse sort the above list:

 # sort -r filename.txt

sort -r filename.txt 2

As you can see, the command sorts the lines in filename.txt in reverse alphabetical order (Z to A).

Similarly, run the following command to sort the contents of a file in reverse numerical order:

# sort -nr numbers.txt

The command sorts the lines in numbers.txt in descending numerical order.

sort -nr numbers.txt

Example #4: Sort by Unique Values

A great use of the sort command is to sort a file and remove duplicate entries. This ensures that each line in the output is unique. 

For this, run the following command:

# sort -u filename.txt

Consider the following screenshot that presents the contents of the file in its original order: 

sort -u filename.txt

Now run the following command that sorts data by unique lines:

 # sort -u filename.txt

Here is the output of the command:

sort -u filename.txt 2

Example #5: Sort by Specific Column

You can use the sort command with files that contain multiple data fields, organized in rows and columns. You can run the following command to sort a file based on a column. 

The syntax of the command is as follows:

# sort -k[column number] [filename]

Here are the contents of the test file:

sort -k[column number] [filename]

Now, consider the following example command that sorts the test file by the contents of the second column of data:

# sort -k2 filename.txt

The command sorts numbers.txt based on the second column, treating those entries as numbers. 

sort -k2 filename.txt

 You can also use the -n flag for numerical sorting, and the -r flag for reverse sorting.

Example #6: Sort by Month

When your file contains month names, and you want to sort them chronologically, run the following command:

# sort -M months.txt

Consider the following contents of the test file: 

sort -M months.txt

Now, run the following command to sort the data, based on months:

 # sort -M months.txt

sort -M months.txt 2

Example #7: Sort the Human-Readable Units

If your file contains numbers with units (like K for thousands, M for millions), you can use the sort command to sort the contents while retaining human-readable format of the entries:

Consider the following image that represents the data in an unsorted format:

Sort the Human-Readable Units

Now, run the following command to sort the data:

# sort -h sizes.txt

sort -h sizes.txt

As you can see, the command takes the human-readable units into consideration when sorting the data. 

Example #8: Ignoring Case While Sorting

By default, the sort command takes the case of the first letter of the lines. However, you can sort without considering the case (uppercase or lowercase) of the text by adding the -f flag in the following syntax: 

# sort -f filename.txt

Consider the following unsorted test file:

sort -f filename.txt

Run the following command to sort data while ignoring the case of letters:

# sort -f filename.txt

sort -f filename.txt (2)

Example #9: Sort the Contents and Output to Another File

A common operational requirement is to sort a file and save the sorted output in another file. The syntax of the command in this scenario will be as follows:

# sort filename.txt -o sorted_filename.txt

Consider the following test file:

sort filename.txt -o sorted_filename.txt

We will now run the following command to sort the contents of the test file and save the output in another file: 

# sort filename.txt -o sorted_filename.txt

We recommend using the cat command to view and verify the contents of the new file.

sort filename.txt -o sorted_filename.txt 2

Example #10: Check If a File Is Already Sorted

You can use the sort command to check if the contents of a file are sorted by adding the -c flag to the command. The syntax of the file will be as follows:

# sort -c filename.txt

Note that this command doesn’t affect the current order of the lines in the file; it merely checks the order and reports if it’s out of order.

Example #11: Sort with a Custom Field Separator

If your file uses a specific character as a field separator (like a comma in CSV files), you can specify it with -t:

The syntax of the command that sorts data containing custom field separator is as follows:

# sort -t [custom field separator] [filename]

Consider the following file contents in unsorted form. Note that the file uses a comma as the field separator:

sort -t [custom field separator] [filename]

The following command uses the second column as the sorting parameter (as indicated by -k2):

# sort -t, -k2 filename.csv

sort -t, -k2 filename.csv

Example #12: Merge Two Sorted Files

You can use the sort command to merge two sorted files into a single file. The syntax of the command is as follows:

# sort -m sorted1.txt sorted2.txt

Consider the following two files: 

sort -m sorted1.txt sorted2.txt

sort -m sorted1.txt sorted2.txt 2

The output will be a single file containing the lines from both files in sorted order:

output of 2 sorted files

These examples showcase the flexibility of the sort command, making it a powerful tool for manipulating text and data in Linux.

Conclusion

The sort command in Linux is an indispensable tool for text and data manipulation. It offers a wide range of options to tailor the sorting process to your needs. Whether organizing data in alphabetical order, distinguishing between uppercase and lowercase letters, or managing the original file carefully, the sort command provides the flexibility and precision required for effective data handling.

For professionals and enthusiasts, managing servers or hosting services can be a daunting task. Fortunately, a reliable hosting partner like RedSwitches significantly streamlined server management tasks. 

FAQs

Q. What is the Linux sort command and how to use it?

The Linux sort command is used to sort lines of text files in ascending order. It can also be used to sort data in specific columns. 

Q. Can you provide an example of using the sort command in Linux?

One of the basic examples of the Linux sort command is sorting the contents of a file in alphabetical order. The following command can be used to achieve this:
sort file.txt

Q. How to sort data in reverse order using the sort command?

To sort data in reverse order using the sort command, use the sort -r option. For example:
# sort -r file.txt

Q. Can you sort numerical values using the sort command in Linux?

Yes, you can sort numerical values using the sort -n option in Linux. For example:
# sort -n numbers.txt

Q. How to create a new file with the sorted output using the Linux sort command?

To create a new file with the sorted output of the Linux sort command, use 

# sort file.txt > sorted_file.txt.

Q. Can you provide some useful examples of the sort command in Linux?

There are many useful examples of the Linux sort command. Some of them include sorting lines in text files, sorting a file based on a specific column, and more.

Q. What is the syntax for sorting the contents of a file in ascending order?

The syntax for sorting the contents of a file in ascending order using the sort command is sort file.txt.

Q. How does the sort command in Linux work?

The sort command in Linux works by sorting the data in the specified file or input and displaying the sorted output in the terminal or redirecting it to a new file.

Q. What are some examples of the sort command with textual explanation?

Some examples of the sort command in Linux include sorting lines in text files and sorting files in reverse order to enhance its functionality. Additionally, the -m option merges pre-sorted files, demonstrating its utility. The sort command also integrates with other tools, such as the cat command to sort contents directly from the command line, making it a powerful utility for data organization at a basic level and beyond.

Try this guide with our instant dedicated server for as low as 40 Euros