sort
Sort lines of text files
By CMD Script Team · 3 min read · Last updated
sort [OPTIONS] [FILE...]Options
| Flag | Description |
|---|---|
-n | Sort numerically instead of lexicographically |
-r | Reverse the sort order |
-k | Sort by a specific field/column, e.g. -k2,2 or -k3n |
-u | Output only unique lines, removing duplicates after sorting |
-t | Set the field separator used by -k (default is whitespace) |
-h | Sort human-readable sizes (e.g. 2K, 1G) in numeric order |
-f | Ignore 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 alphabeticallysort -n scores.txt— sort numericallysort -r file.txt— sort in reverse (descending) ordersort -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
-nand 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
-k2alone when you mean-k2,2— without the end field,sortsorts by field 2 through the end of the line, not just field 2. - Not setting
-twhen the data isn't whitespace-delimited, causing-kto select the wrong column entirely on CSV or colon-delimited files.
Tips
- Always pair
sort -kwith an explicit end field (-k2,2) unless you deliberately want to sort by field 2 onward. - Use
sort -hfor human-readable sizes fromdu -shorls -lhso "1K" doesn't sort after "500K". sort -Rrandomizes line order — handy for sampling or shuffling test data (note: not cryptographically random).
Best practices
- Explicitly state numeric vs. lexicographic intent (
-nor not) rather than relying on default behavior, especially in scripts other people will read. - When sorting structured data (CSV,
/etc/passwd), always pin down-tand a precise-krange instead of guessing from default whitespace splitting. - Use
sort -uinstead ofsort | uniqwhen 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 -10to 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
joincommand, which requires sorted input on the join key:sort -k1,1 file1.txt > file1.sorted.
Common interview questions
- Why does
sortput "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— orsort -k3,3nif the column is numeric; the,3bounds the key to just that field. - Why does
sortneed to run beforeuniq?uniqonly removes consecutive duplicate lines;sortgroups 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.