Vol. I · No. 46The command line sectionSat, 10 Oct 2026
Linux · text processing

sort

put lines in order

sort reads lines and prints them in order. By default that is alphabetical, which puts 10 before 9; -n sorts by number and -h understands sizes such as 2K and 1G.

Text order is not number order: use -n for numbers and -h for sizes.

Default
Alphabetical
character by character
Numbers
-n
or -h for 2K, 1G
By column
-k NUM
with -t for the separator
Reverse
-r
largest or last first

At the terminal

Read the session first, then the notes: each numbered line in the output is answered by the note with the same number.

user@netflow — bashcoreutils
$ sort sizes.txt
110
200
9
2$ sort -n sizes.txt
9
10
200
3$ sort -t, -k4 -nr data.csv | head -2
7,Meera,Engineering,98000
1,Asha,Engineering,72000
Illustrative session · names, sizes and dates are examples, and every address is from a documentation or private range
Reading the output

What the numbered lines are telling you

1
Alphabetical order

Compared as text, "10" comes before "9" because the character 1 comes before 9.

2
-n compares as numbers

Now 9 is first. This is almost always what is wanted for a column of figures.

3
Sorting by a column

-t, makes the comma the separator and -k4 sorts on the fourth field. -n is numeric and -r reverses: highest salary first.

Switches

A switch changes how the command behaves. These are the ones used day to day; man sort lists the rest. Single-letter switches can usually be joined, so -l -a may be written -la.

-n
Numeric: compare as numbers.
-h
Human-numeric: understand sizes such as 2K and 1G.
-r
Reverse the order.
-u
Unique: drop duplicate lines.
-k NUM
Sort by field number NUM.
-t CHAR
Use this character as the field separator.
-f
Fold case: treat upper and lower case alike.

Variations worth knowing

The same tool, pointed at a narrower question. These four cover most of what it is reached for day to day.

du -sh * | sort -h

Sorts readable sizes correctly, so 900K comes before 2M.

sort -u names.txt

Sorts and removes duplicate lines in one step.

sort -k2 file.txt

Sorts on the second whitespace-separated field.

sort access.log | uniq -c | sort -nr | head

The classic pipeline: the ten most frequent lines.

In cybersecurity

Sorting is the first step in making sense of a log.

Finding the most active address

Extract the address column from an access log, sort it, count with uniq -c and sort by the count: the top talkers appear first.

Sorted lists can be compared

diff and comm need sorted input. Sorting two lists of users or packages is what makes comparing them possible.

Locale changes the order

Sorting rules depend on the language setting, so two machines can sort the same file differently. LC_ALL=C sort gives a fixed byte order.

Filed under

Text processing

Read alongside