Skip to main content

Understanding the `sort` Command in Linux

In Linux, the sort command is a text processing utility that arranges lines in text and output files. It sorts lines of text alphabetically or numerically, based on a field or character position, making it a versatile tool for managing text-based data.

Syntax

The general syntax for the sort command is:

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

Here, [OPTION] specifies how the sort command should behave, while [FILE] denotes the files that need to be sorted. If no file is provided, sort will read from the standard input.

Options Table

OptionShorthandDescription
--dictionary-order-dConsider only blanks and alphanumeric characters.
--ignore-case-fPerform case-insensitive sorting.
--numeric-sort-nCompare according to numerical value.
--reverse-rReverse the sort, i.e., highest to lowest.
--unique-uOutput only the first of an equal run.
--key-kDefine a key field to sort by.
--output-oOutput file name.
--helpDisplay the help message and exit.
--versionDisplay the version information and exit.

Basic Usage

Alphabetical Sorting

Create a text file named names.txt with the following content:

Eva
David
Alice
Frank
Cathy

Use the sort command to sort the lines alphabetically:

sort names.txt

Output:

Alice
Cathy
David
Eva
Frank

Numerical Sorting

Create a file named numbers.txt:

15
9
100
32

To sort the numbers, use the -n option:

sort -n numbers.txt

Output:

9
15
32
100

Case-Insensitive Sorting

For a case-insensitive sort, use the -f option. Assuming names.txt has:

Eva
david
Alice
Frank
CATHY

Run:

sort -f names.txt

Output:

Alice
CATHY
david
Eva
Frank

Advanced Usage

Sorting By Column

Create a file called students.txt:

Alice 85
David 92
Eva 78
Frank 90

To sort the students by scores:

sort -k2 -n students.txt

Output:

Eva 78
Alice 85
Frank 90
David 92

Unique Sorting

To sort a file while eliminating duplicate lines, use the -u option. Given a duplicates.txt file:

apple
orange
apple
banana

Run:

sort -u duplicates.txt

Output:

apple
banana
orange

Combining Multiple Options

You can combine multiple options for a more complex sort. To sort students.txt numerically by scores in descending order:

sort -k2 -n -r students.txt

Output:

David 92
Frank 90
Alice 85
Eva 78

Conclusion

The sort command is a powerful text-processing utility for organizing lines in text files based on various criteria like alphabetical order, numerical value, and even complex key-based sorting. It is often used in combination with other text processing commands like awk, grep, and cut to create powerful text manipulation pipelines. Learning how to use sort effectively can significantly enhance your text processing skills in Linux.

What Can You Do Next 🙏😊

If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.

YouTube @cloudaffle