>_cmd.script

sort

Sort lines of text files

Text Processing

By CMD Script Team · 3 min read · Last updated

SYNTAX
sort [OPTIONS] [FILE...]

Options

Command options and flags
FlagDescription
-nSort numerically instead of lexicographically
-rReverse the sort order
-kSort by a specific field/column, e.g. -k2,2 or -k3n
-uOutput only unique lines, removing duplicates after sorting
-tSet the field separator used by -k (default is whitespace)
-hSort human-readable sizes (e.g. 2K, 1G) in numeric order
-fIgnore case when comparing lines

Distribution compatibility

  • Ubuntu
  • Debian
  • Fedora
  • Arch
  • macOS

What it does

sort reads lines from files or standard input and writes them back out in sorted order. By default it sorts lexicographically (byte by byte), but it supports numeric, reverse, and field-based sorting, and can also filter out duplicate lines as part of the sort. It's the standard building block for ordering data in shell pipelines, and pairs constantly with uniq and cut.

Beginner examples

  • sort file.txt — sort lines alphabetically
  • sort -n scores.txt — sort numerically
  • sort -r file.txt — sort in reverse (descending) order
  • sort -u file.txt — sort and drop duplicate lines
sort -n ages.txt

Advanced examples

  • Sort by a specific field, treating it as numeric: sort -k2,2n employees.txt
  • Sort a CSV file by its third column: sort -t',' -k3,3 data.csv
  • Sort multiple keys — by department, then by salary descending: sort -k1,1 -k2,2nr payroll.txt
  • Sort human-readable disk usage output correctly (2K before 10M): du -sh * | sort -h
  • Case-insensitive alphabetical sort: sort -f names.txt
sort -t':' -k3,3n /etc/passwd | head

Common mistakes

  • Sorting a list of numbers without -n and getting "1, 10, 11, 2, 3" instead of "1, 2, 3, 10, 11" because the default comparison is lexicographic.
  • Forgetting to sort before piping into uniq, so duplicate lines that aren't adjacent don't get collapsed.
  • Using -k2 alone when you mean -k2,2 — without the end field, sort sorts by field 2 through the end of the line, not just field 2.
  • Not setting -t when the data isn't whitespace-delimited, causing -k to select the wrong column entirely on CSV or colon-delimited files.

Tips

  • Always pair sort -k with an explicit end field (-k2,2) unless you deliberately want to sort by field 2 onward.
  • Use sort -h for human-readable sizes from du -sh or ls -lh so "1K" doesn't sort after "500K".
  • sort -R randomizes line order — handy for sampling or shuffling test data (note: not cryptographically random).

Best practices

  • Explicitly state numeric vs. lexicographic intent (-n or not) rather than relying on default behavior, especially in scripts other people will read.
  • When sorting structured data (CSV, /etc/passwd), always pin down -t and a precise -k range instead of guessing from default whitespace splitting.
  • Use sort -u instead of sort | uniq when you only need unique output — it's a single pass and slightly more efficient.

Try it yourself

A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.

Real-world use cases

  • Ranking processes or files by size: du -sh * | sort -rh | head -10 to find the largest directories.
  • Deduplicating and ordering a list of email addresses before a mail merge: sort -u emails.txt.
  • Preparing two files for a join command, which requires sorted input on the join key: sort -k1,1 file1.txt > file1.sorted.

Common interview questions

  • Why does sort put "10" before "2" without -n? Default sort order is lexicographic (character-by-character comparison), so "1" is compared to "2" and found smaller, regardless of the full numeric value.
  • How do you sort a file by its third column? sort -k3,3 file — or sort -k3,3n if the column is numeric; the ,3 bounds the key to just that field.
  • Why does sort need to run before uniq? uniq only removes consecutive duplicate lines; sort groups identical lines together first so all duplicates become adjacent and can be collapsed.

Frequently Asked Questions

Why does sort put '10' before '2' by default?

Without -n, sort compares lines lexicographically (character by character), so '10' sorts before '2' because '1' < '2'. Use sort -n for numeric ordering.

How do I sort by a specific column?

Use -k to specify the field, e.g. sort -k2,2 file sorts by the second field only. Combine with -t to set the delimiter if fields aren't whitespace-separated, e.g. sort -t',' -k3,3n data.csv sorts numerically by the third CSV column.

Why do I need to sort before uniq?

uniq only removes adjacent duplicate lines, not duplicates anywhere in the file. sort groups identical lines together first so uniq can then collapse them correctly — sort file | uniq is the standard pattern.

How do I sort in reverse numeric order?

Combine -n and -r: sort -nr file.txt sorts numerically from largest to smallest.

Cheat sheet

Download a quick-reference cheat sheet for sort.