sort Command in Linux



The sort command under Linux is a very handy utility that helps you sort data in an organized, clean way. It is helpful for tasks like sorting names alphabetically, sorting numbers, or even sorting data in table form based on sort columns. It processes text files line by line to make it easier and faster to manipulate and understand information.

Assume that you have a messy dataset with all the entries mixed up or with unnecessary lines. The sort tool can put everything in order and get rid of the mess. You can sort by various things, like in ascending order, descending order, or even set your own criteria, and you can even remove duplicates. This tool can convert a messy file into a clean file in an instant, which is much more convenient to work with.

Table of Contents

Here is a comprehensive guide to the options available with the sort command −

Syntax for the sort Command

The basic syntax for the sort command is as follows −

sort [options] [file]
  • [options] − Flags that modify how the sorting process is carried out.
  • [file] − The name of the file that you want to sort. If no file is specified, sort reads input directly from the keyboard or another program’s output.

Details Provided by the sort Command

When you use the sort command, it arranges the lines of text according to specific criteria, providing the following advantages −

  • Alphabetical Sorting − It sorts text lines alphabetically.
  • Numeric Sorting − It manages numerical values and sorts them in ascending or descending order.
  • Case Sensitivity − By default, uppercase letters are considered "smaller" than lowercase, but this behavior can be modified with options.
  • Stability − It preserves the relative order of lines that are already identical based on the sorting criteria.

Options Available for sort Command

The sort command comes with several options that allow users to adjust its behavior −

Options Description
-b, --ignore-leading-blanks Ignores any spaces or blank characters at the beginning of each line when sorting. This helps to focus only on the actual text or data.
-d, --dictionary-order Considers only spaces and alphanumeric characters when sorting, ignoring special characters and punctuation.
-f, --ignore-case Treats uppercase and lowercase letters as the same, sorting them together.
-g, --general-numeric-sort Sorts numbers based on their actual value, including those in decimal or exponential formats.
-i, --ignore-nonprinting Focuses only on printable characters, ignoring any unprintable ones in the input.
-M, --month-sort Sorts by month names (e.g., JAN to DEC) in chronological order.
-h, --human-numeric-sort Sorts numbers written in a human-readable format, like 2K, 1G, or 500M.
-n, --numeric-sort Sorts numerical data based on the string value of numbers.
-R, --random-sort Shuffles the lines randomly, while grouping identical lines together.
--random-source=FILE Uses a custom file as the source for randomization.
-r, --reverse Reverses the sorting order, making the largest or last entries appear first.
--sort=WORD Allows sorting based on specific criteria. eplace WORD with options like general-numeric (-g), human-numeric (-h), month (-M), numeric (-n), or random (-R).
-V, --version-sort Arranges text containing version numbers naturally (e.g., v1.2 comes before v1.10).

Additional Options

Here are some extra features that expand the functionality of the sort command −

Option Description
--batch-size=NMERGE Limits the number of files merged at a time. If exceeded, temporary files are used.
-c, --check Verifies whether the input is already sorted.
--debug Highlights which parts of a line are used during sorting, displaying warnings for invalid usage.
-k, --key=KEYDEF Sorts based on a specific column or field in a file.

Examples of sort Command in Linux

Let’s explore some practical examples to understand how to use the sort command in real scenarios −

  • Ignoring Leading Blanks
  • Sorting in Dictionary Order
  • Sorting with Reversed Order
  • Numeric Sorting
  • Month-Based Sorting

Ignoring Leading Blanks

To sort a file while ignoring blank spaces at the beginning of lines, use the -b option −

sort students.txt

If students.txt contains −

Kumar
Sahir
Roger
Milind

The command will produce −

sort Command in Linux1

Sorting in Dictionary Order

To sort a file while ignoring special characters and focusing on alphanumeric ones, use the -d option −

sort -d words.txt

For a file words.txt with the following content −

Hello!
#Goodbye
Welcome to Tutorials Point Website
Apple
@Banana

The output will be −

sort Command in Linux2

Sorting with Reversed Order

To sort the file names.txt in descending (reverse) alphabetical order −

sort -r names.txt
sort Command in Linux3

Ignoring Case Sensitivity

To sort text in a case-insensitive manner, use the -f option −

sort -f names.txt

Given this file names.txt −

alice
James
Maddy
Eva
Mac

The sorted output will look like −

sort Command in Linux4

Numeric Sorting

When working with numbers, sort them based on their value instead of treating them as text with the -n option −

sort -n numbers.txt

If numbers.txt contains −

10
12
5
15

The output will be −

sort Command in Linux5

Month-Based Sorting

To sort data based on month names (e.g., JAN, FEB, etc.), use the -M option −

sort -M months.txt

If months.txt contains −

Dec
Jan
Mar
Nov
Sep

The sorted output will be −

sort Command in Linux6

Random Shuffling

If you want to shuffle the lines in a file randomly, use the -R option −

sort -R items.txt

For a file items.txt

Apple
Grapes
Cherry
Date

Each time you run the command, the output will look different, for instance −

sort Command in Linux7

Conclusion

The sort command is a simple yet powerful one in the Linux universe, perfect for cleaning up and organizing data. With its many available options and versatility, it's one of the most popular commands for working with lists, tables, and text files. Whether you need to sort alphabetically, numerically, or based on particular columns, this command makes the job easier and speeds things up.

Mastering the sort command gives you a useful skill to simplify data management, keeping your work processes running smoothly and in good order.

Advertisements