Skip to content

The sort command

Sort lines of text. Prints the lines of the files (or of what is piped into it) in order. Upper and lower case sort together, like a dictionary.

Usage

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

Options

-n, --numeric-sortsort by the number at the start of each line, so 9 comes before 10
-r, --reversereverse the order
-u, --uniqueprint each distinct line once
-f, --ignore-casetreat upper and lower case as the same when comparing
-k Nsort by the Nth word of each line instead of the whole line

Examples

  • sort names.txtAlphabetical order
  • sort -n scores.txtSmallest number first
  • sort fruit.txt | uniq -c | sort -nrCount each line, most common first

Learn it step by step

learner@sandbox: ~bash (practice)
Note: Try sort here: your home directory has sample files. Press Try beside an example, or type your own.

A practice shell in your browser that behaves like bash on Linux for these commands. Nothing you type leaves the page.