Fundamentals 7 min read

Mastering Linux sort: Practical Options for Numeric, Human‑Readable, and Custom Sorting

This guide explains how to use the Linux sort command with various options—including numeric (-n), human‑readable (-h), month (-M), check (-c), reverse (-r), unique (-u), and field‑based sorting (-k, -t, -o)—through clear examples and command‑line demonstrations.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Mastering Linux sort: Practical Options for Numeric, Human‑Readable, and Custom Sorting

The sort command sorts lines in a text file, either displaying the result on the screen or redirecting it to another file. Various command‑line options tailor the sorting behavior to different data types and requirements.

Basic Syntax

$ sort [options] file

Example file test:

$ cat test
zzz
sss
qqq
aaa
BBB
ddd
AAA

Running sort test produces an alphabetically sorted output.

1. Numeric Sorting (-n)

Use -n or --numeric-sort to sort by leading numbers.

$ cat test
22 zzz
33 sss
11 qqq
77 aaa
55 BBB
$ sort -n test
11 qqq
22 zzz
33 sss
55 BBB
77 aaa

2. Human‑Readable Numeric Sorting (-h)

Use -h or --human-numeric-sort for values like 1K, 2M, 1G.

$ cat test
2K
2G
1K
6T
1T
1G
2M
$ sort -h test
1K
2K
2M
1G
2G
1T
6T

3. Month Sorting (-M)

Use -M or --month-sort to order month abbreviations (jan, feb, …). At least three characters are required for each month name.

$ cat test
sept
aug
jan
oct
apr
feb
mar11
$ sort -M test
jan
feb
mar11
apr
aug
sept
oct

4. Check If Already Sorted (-c)

Use -c or --check to verify sorting; it reports the first disorder.

$ cat test
2
5
1
6
$ sort -c test
sort: test:3: disorder: 1

5. Reverse and Unique Output (-r, -u)

-r or --reverse reverses the order; -u removes duplicate lines.

$ cat test
5
2
2
1
4
4
$ sort -r test
5
4
4
2
2
1
$ sort -r -u test
5
4
2
1

6. Field‑Based and Custom Delimiter Sorting (-k, -t, -o)

Use -k to sort by a specific column, -t to define a custom delimiter, and -o to write output to a file.

$ cat test
aa aa zz
aa aa ff
aa aa tt
aa aa kk
$ sort -k3 test
aa aa ff
aa aa kk
aa aa tt
aa aa zz

With a pipe delimiter:

$ cat test
aa|5a|zz
aa|2a|ff
aa|1a|tt
aa|3a|kk
$ sort -n -t '|' -k2 test -o outfile

Resulting outfile:

$ cat outfile
aa|1a|tt
aa|2a|ff
aa|3a|kk
aa|5a|zz
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Linuxtext processingShell scriptingcommand-linesort command
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.