Fundamentals 7 min read

Mastering Linux sort: Essential Options and Real‑World Examples

This guide explains the Linux sort command, detailing its most useful options, how to sort by numbers, fields, human‑readable sizes, remove duplicates, and write results to files, with clear command‑line examples for each scenario.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Mastering Linux sort: Essential Options and Real‑World Examples

Overview

The sort utility on Linux provides powerful text‑sorting capabilities. It can order lines alphabetically, numerically, by specific fields, or using human‑readable units, and offers options for case‑insensitivity, reverse order, uniqueness, and output redirection.

Common Options

-n – number‑sort : sort based on numeric string values (does not treat numbers as floating‑point).

-g – general‑number‑sort : sort using general numeric comparison, supporting scientific notation.

-f – ignore‑case : perform case‑insensitive sorting.

-k – key=POS1[,POS2] : sort starting at field POS1; optionally end at POS2.

-t – field‑separator=SEP : define the column delimiter.

-r – reverse : output in descending order.

-h – human‑numeric‑sort : understand human‑readable numbers such as 2K, 1G.

-u – unique : suppress duplicate lines.

-o – output=FILE : write the sorted result directly to FILE.

Typical Usage Examples

1. Default alphabetical sort

[guodong@proxy ~]$ cat word.txt
one
two 
three
four

[guodong@proxy ~]$ sort word.txt
four
one 
three
two

2. Numeric sort with -n

[guodong@proxy ~]$ cat num.txt
100
20
3

[guodong@proxy ~]$ sort num.txt -n
3
20
100

3. Sorting by a specific column

Using -t ':' to split fields and -k 3 to sort by the third column numerically in reverse order:

[guodong@proxy ~]$ cat passwd
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
... (other lines omitted) ...

[guodong@proxy ~]$ sort -t ':' -k 3 -nr passwd
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
...

4. Sorting directory sizes from du -h

[guodong@proxy ~]$ du -h
2.0G    ./test2
4.0K    ./test3
316M    ./test
2.3G    .

[guodong@proxy ~]$ du -h | sort -hr
2.3G    .
2.0G    ./test2
316M    ./test
4.0K    ./test3

5. Finding top memory‑consuming processes

[guodong@proxy ~]$ ps aux|sort -gr -k 4|head -n 5
shuanghu  1740 15.7  4.6 1506764 189872 ?      Sl    5月07 142:08 compiz
root      1304  2.1  1.9 338928 80208 tty7     Ssl+  5月07  19:29 /usr/bin/X -core ...
...

6. Removing duplicate lines

[guodong@proxy ~]$ cat word.txt
one
two
two
three
three
three
four
four
four

[guodong@proxy ~]$ sort -u word.txt
four
one
three
two

7. Writing sorted output back to a file

Direct redirection overwrites the source file, leaving it empty. Use -o instead:

[guodong@proxy ~]$ sort word.txt > word.txt   # results in an empty file
[guodong@proxy ~]$ sort word.txt -o word.txt   # correctly writes sorted data
[guodong@proxy ~]$ sort -u word.txt
four
one
three
two
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.

Shellcommand-linesorttext-processing
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.