ZetCode

Linux sort Command

last modified October 23, 2026

The sort command in Linux sorts lines of text. It reads files or standard input and writes the sorted result to standard output. Lines can be ordered alphabetically, numerically, by month names, by human-readable sizes, or by version numbers, and the sort key can be any field of a line.

sort is commonly used for organizing data, removing duplicates, and preparing input for other tools such as uniq, join, or comm. The examples in this article use GNU sort from coreutils and the en_US.UTF-8 locale.

Basic sorting

By default, sort compares whole lines and orders them alphabetically according to the current locale.

sort words.txt

We first print the unsorted contents of words.txt and then sort it. The original file is not modified; the sorted lines go to standard output. Duplicate lines are kept.

$ cat words.txt
sky
forest
falcon
rock
bear
sky
wolf
book
falcon
owl
$ sort words.txt
bear
book
falcon
falcon
forest
owl
rock
sky
sky
wolf

Reverse sorting

The -r option reverses the result of the comparison.

sort -r words.txt

The lines are printed in descending order.

$ sort -r words.txt
wolf
sky
sky
rock
owl
forest
falcon
falcon
book
bear

Numeric sorting

Text sorting compares characters, not values, so numbers end up in an unexpected order. The -n option compares lines by their numeric value.

sort -n numbers.txt

Without -n, 100 comes before 2 because the character 1 sorts before 2; the locale also ignores punctuation such as the minus sign and the decimal point. With -n, negative and decimal numbers are ordered correctly.

$ sort numbers.txt
10
100
1.5
2
33
4
-5
7
$ sort -n numbers.txt
-5
1.5
2
4
7
10
33
100

Removing duplicates

The -u option outputs only the first of a run of equal lines.

sort -u words.txt
sort words.txt | uniq -c

sort -u is equivalent to sort | uniq. When we also need to know how many times each line occurs, we pipe the sorted output to uniq -c, which prefixes each line with its count. The uniq command only merges adjacent lines, which is why the input must be sorted first.

$ sort -u words.txt
bear
book
falcon
forest
owl
rock
sky
wolf
$ sort words.txt | uniq -c
      1 bear
      1 book
      2 falcon
      1 forest
      1 owl
      1 rock
      2 sky
      1 wolf

Case and locale

The ordering of uppercase and lowercase letters depends on the locale. The -f option folds lowercase letters to uppercase for comparison.

sort fruits.txt
LC_ALL=C sort fruits.txt
LC_ALL=C sort -f fruits.txt

In the en_US.UTF-8 locale, case is a secondary criterion, so apple and Apple stay together. In the C locale, lines are compared byte by byte and all uppercase letters come before lowercase ones. Adding -f makes the C locale sort case-insensitively. Setting LC_ALL=C is also a common way to get fast, reproducible results in scripts.

$ sort fruits.txt
apple
Apple
banana
Banana
cherry
$ LC_ALL=C sort fruits.txt
Apple
Banana
apple
banana
cherry
$ LC_ALL=C sort -f fruits.txt
Apple
apple
Banana
banana
cherry

Sorting by a column

The -k option selects the sort key. By default, fields are separated by blanks.

sort -k 2 employees.txt
sort -k 2n employees.txt

The file contains a name, a salary, and a city. -k 2 sorts by the text starting at the second field, so 980 ends up last. Appending n to the key definition applies numeric comparison to that key only. Lines with equal keys are compared as whole lines.

$ sort -k 2 employees.txt
Anna 1850 London
Mark 1850 Paris
Paul 2400 London
John 3100 Berlin
Lucy 980 Paris
$ sort -k 2n employees.txt
Lucy 980 Paris
Anna 1850 London
Mark 1850 Paris
Paul 2400 London
John 3100 Berlin

Multiple sort keys

A key definition of the form -k start,end limits the key to a range of fields. Multiple -k options define secondary keys used to break ties.

sort -k3,3 -k2,2nr employees.txt

The employees are sorted by city. Within the same city, they are sorted by salary in descending order; the nr modifiers apply only to the second key. Without the ,3 end position, the first key would extend to the end of the line.

$ sort -k3,3 -k2,2nr employees.txt
John 3100 Berlin
Paul 2400 London
Anna 1850 London
Mark 1850 Paris
Lucy 980 Paris

Custom field separator

The -t option sets the field separator, which is needed for CSV and similar formats.

tail -n +2 products.csv | sort -t, -k3,3nr

The tail -n +2 command skips the header line. The products are then sorted by the third comma-separated field, the price, from the most expensive to the cheapest.

$ tail -n +2 products.csv | sort -t, -k3,3nr
2,monitor,189.99
4,headset,59.90
1,keyboard,45.50
5,webcam,39.00
3,mouse,12.00

Sorting by month names

The -M option sorts abbreviated month names in calendar order.

sort -M months.txt

The comparison recognizes names such as Jan or JAN; unknown strings come before Jan.

$ sort -M months.txt
Jan
Feb
Mar
Jul
Oct
Dec

Sorting human-readable sizes

The -h option compares numbers with SI suffixes such as K, M, and G, as printed by du -h or ls -lh.

sort -h sizes.txt
sort -hr sizes.txt | head -n 2

The first command lists the directories from the smallest to the largest. The second one combines -h with -r to show the two largest entries. A typical use is du -sh * | sort -hr.

$ sort -h sizes.txt
4.0K	config
200K	notes
12M	photos
850M	videos
1.5G	backups
$ sort -hr sizes.txt | head -n 2
1.5G	backups
850M	videos

Sorting version numbers

The -V option performs a natural sort of version numbers within text.

sort versions.txt
sort -V versions.txt

A plain sort puts 1.10.0 before 1.2.0. With -V, the numeric parts are compared as numbers, so the releases are listed in the correct order.

$ sort versions.txt
app-1.10.0
app-1.10.1
app-1.2.0
app-1.9.3
app-2.0.0
$ sort -V versions.txt
app-1.2.0
app-1.9.3
app-1.10.0
app-1.10.1
app-2.0.0

Writing the output to a file

The -o option writes the result to a file instead of standard output.

sort -o sorted.txt words.txt
sort -o words.txt words.txt

Unlike shell redirection, -o may name one of the input files, so it can sort a file in place. The command sort words.txt > words.txt would truncate the file before sort reads it and leave it empty.

$ sort -o sorted.txt words.txt
$ cat sorted.txt
bear
book
falcon
falcon
forest
owl
rock
sky
sky
wolf
$ sort -o words.txt words.txt
$ head -n 3 words.txt
bear
book
falcon

Checking if input is sorted

The -c option does not sort anything; it checks whether the input is already sorted.

sort -c numbers.txt || echo "not sorted"
sort -n numbers.txt | sort -nc && echo "sorted"

sort -c reports the first out-of-order line and exits with status 1. The check uses the same ordering options as a sort, so the numeric output of the first sort passes the -nc check.

$ sort -c numbers.txt || echo "not sorted"
sort: numbers.txt:5: disorder: 100
not sorted
$ sort -n numbers.txt | sort -nc && echo "sorted"
sorted

Combining sort with other commands

sort is often part of a pipeline. A classic idiom counts occurrences and ranks them by frequency.

cut -d" " -f3 employees.txt | sort | uniq -c | sort -rn

The cut command extracts the city names. The first sort groups equal names together for uniq -c, and the second one ranks the counts in descending numeric order.

$ cut -d" " -f3 employees.txt | sort | uniq -c | sort -rn
      2 Paris
      2 London
      1 Berlin

Best practices for sort

Source

GNU sort manual

In this article, we have used the sort command to order lines alphabetically, numerically, by fields, by month names, by sizes, and by version numbers.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Linux tutorials.