Linux Command - sort

sort - Sort lines of text files.

Syntax

1
sort [OPTIONS] [FILE]...

Options

Common Option Description
-f, --ignore-case fold lower case to upper case characters
-n, --numeric-sort compare according to string numerical value
-r, --reverse reverse the result of comparisons
-k, –key=KEYDEF sort via a key; KEYDEF gives location and type
-u, –unique remove redundent

Examples

Sort lines in a file

1
sort file1

sample file1

1
2
3
xyz
abcd
ttt

sample output

1
2
3
abcd
ttt
xyz

Sort lines in a file desc

1
sort -r file1

find uniqe words and sort

1
cat words.txt | sort -u

Show processes sorted by user name

use -k 1 to sort the processes by first field, which is the user name.

1
ps aux | sort -k 1 | head -n 10

Show processes sorted by CPU Usage desc

1
ps aux | sort -r -k 4 -n | head -n 10

Reference