UnixDroid

Text · Linux command

sort command in Linux

What does sort do?

sort puts lines in order — alphabetically by default, numerically with -n, reversed with -r.

sort [options] [file]

Most useful options

OptionWhat it does
-nnumeric
-rreverse
-uremove duplicates
-k 2sort by column 2
-hhuman sizes (2K, 1G)

sort examples

$ sort names.txt Try it ›

A to Z

$ sort -nr counts.txt Try it ›

biggest number first

$ sort | uniq -c | sort -nr Try it ›

the classic "top N" recipe

In security work

sort | uniq -c | sort -nr finds the top attacking IPs in seconds.

Questions about sort

Why do I need sort before uniq?

uniq only removes duplicates that are next to each other, so sort first.