Operations 7 min read

Master Linux ‘sort’: Essential Options and Real-World Examples

This guide explains the Linux sort command, covering its most useful options—such as numeric, human‑readable, case‑insensitive, field‑based, and reverse sorting—and demonstrates practical examples for default sorting, column sorting, deduplication, and writing results to files.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Linux ‘sort’: Essential Options and Real-World Examples

The Linux sort command provides powerful sorting capabilities for text data. Below are the most frequently used options:

-n – number‑sort: sorts by numeric string values without converting to floating‑point numbers.

-g – general‑number‑sort: sorts by general numeric values and supports scientific notation.

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

-k POS1[,POS2] – key: sorts starting at field POS1; if POS2 is provided, sorting ends at POS2.

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

-r – reverse: sorts in descending order (default is ascending).

-h – human‑numeric‑sort: sorts numbers with human‑readable suffixes (e.g., 2K, 1G).

-u – unique: removes duplicate lines.

-o FILE – output: writes the sorted result to the specified file.

Common Usage Examples

1. Default (alphabetical) sorting

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

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

2. Numeric sorting

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

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

3. Column‑based sorting

Sorting a colon‑separated file by the third column in descending numeric 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
... (remaining lines)

4. Human‑readable size sorting

Using du -h output and sorting with -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. Sorting processes by memory usage

[guodong@proxy ~]$ ps aux | sort -gr -k 4 | head -n 5
shuanghu  1740 15.7  4.6 ... compiz
root       1304  2.1  1.9 ... /usr/bin/X ...
... (other lines omitted)

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 to a file

Using the -o option instead of redirection:

[guodong@proxy ~]$ sort word.txt -o word.txt
[guodong@proxy ~]$ cat 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.

Linuxcommand-linesorttext-processing
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.