Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

sort

Put lines in order: alphabetical, by numbers, by sizes, by versions, by a chosen column.

Level
RHCSA exam
Group
Text and search
Needs root
No
Package
coreutils

What it does

sort reads all lines and prints them sorted. By default it sorts as text, from A to Z (following the language settings). With options it sorts numbers (-n), human sizes (-h), versions (-V), in reverse (-r), or on one column (-k).

Combined with uniq -c it makes "top lists" of anything: the most common IPs, errors, users.

How to write it

sort [OPTION]... [FILE]...

Options 31

Most used

How to compare

-n, --numeric-sort
Sort as numbers (2 before 10).
-h, --human-numeric-sort
Sort human sizes like 2K, 300M, 1G.
-V, --version-sort
Sort version numbers in a natural way (v1.9 before v1.10).
-g, --general-numeric-sort
Sort numbers with exponents (1e3).
-M, --month-sort
Sort month names (JAN < FEB …).
-R, --random-sort
Shuffle the lines (equal lines stay together).
--random-source=file
Random bytes for -R from this file.
-f, --ignore-case
Ignore upper/lower case.
-d, --dictionary-order
Look only at letters, digits and blanks.
-i, --ignore-nonprinting
Ignore non-printable characters.
-b, --ignore-leading-blanks
Ignore spaces at the start.
--sort=word
Same as the options above: general-numeric, human-numeric, month, numeric, random, version.

What to compare

-k, --key=keydef
Sort on a field: -k2 = from field 2, -k2,2 = only field 2, -k3n = field 3 as a number.
-t, --field-separator=sep
The field separator (default: change from non-blank to blank).

Order and output

-r, --reverse
Reverse the order.
-u, --unique
Print only the first of equal lines.
-s, --stable
Keep the input order for equal lines.
-o, --output=file
Write the result to a file (can be the same as the input file).
-c, --check
Check if the input is sorted; report the first wrong line.
-C, --check=quiet
Check silently (only the exit code).
-m, --merge
Merge files that are already sorted.
-z, --zero-terminated
Lines end with NUL.

Performance

-S, --buffer-size=size
Memory to use, like 1G.
-T, --temporary-directory=dir
Folder for temporary files.
--parallel=n
Sort with N threads.
--batch-size=n
Merge at most N inputs at once.
--compress-program=prog
Compress temporary files with this program.
--files0-from=file
Read input file names from a file (NUL separated).
--debug
Show which part of each line is used for sorting.
--help
Show help.
--version
Show the version.

Examples 12

$ run as a normal user# run as root (or with sudo)

  1. 1Sort lines alphabetically

    sort names.txt

    A to Z.

  2. 2Sort numbers the right way

    sort -n numbers.txt

    Without -n, 10 comes before 9 (text order).

  3. 3Biggest folders last (human sizes)

    du -sh /var/* 2>/dev/null | sort -h

    -h understands K, M and G.

  4. 4Sort /etc/passwd by UID

    sort -t: -k3,3n /etc/passwd

    Field separator :, key = field 3 only, as a number.

  5. 5Top list of anything

    cut -d: -f7 /etc/passwd | sort | uniq -c | sort -rn

    Count each value, the most common first.

  6. 6The IPs with the most requests in a web log

    awk '{print $1}' /var/log/httpd/access_log | sort | uniq -c | sort -rn | head

    The first step when a site is slow or attacked.

  7. 7Unique values only

    sort -u hosts-from-many-files.txt

    Same as sort | uniq.

  8. 8Sort version numbers

    ls /srv/releases | sort -V

    v1.2, v1.10, v2.0 in the right order.

  9. 9Sort by the second column, reverse

    sort -k2,2nr scores.txt

    Highest score first.

  10. 10Sort a file in place

    sort -o users.txt users.txt

    -o can write to the same file safely (redirect > cannot).

  11. 11Check if a file is sorted

    sort -c list.txt && echo sorted

    Exit code 0 when sorted.

  12. 12Sort a huge file faster

    sort -S 2G --parallel=4 -T /var/tmp big.csv > big-sorted.csv

    More memory and threads; temp files on a disk with space.

Watch out

  • Text order depends on the language (LANG). For a byte-order sort in scripts use LC_ALL=C sort.
  • sort file > file empties the file first. Use -o file.
  • Without ,N in -k, the key goes to the end of the line: use -k2,2 for only field 2.

Tips to remember

  • n = numbers, h = sizes, V = versions, r = reverse, u = unique, k = column, t = separator.