Quick take: The sort command orders lines of text. Use -n for numeric sorting, -r to reverse, -k to sort by a specific column, and -u to remove duplicates while sorting.
Introduction
The sort command arranges lines of input into order — alphabetical by default, or numeric, reverse, and by column with the right flags. It is a pipeline staple, used to organise output, prepare data for uniq, and rank results.
This guide covers the common sort modes, sorting by a chosen field, and combining sort with other commands to produce ranked and de-duplicated output.
Syntax
The basic syntax of the sort command is:
sort [OPTIONS] [FILE...]Common Options and Parameters
The most useful options and parameters for the sort command:
| Option | Description |
|---|---|
| -n | Sort numerically rather than alphabetically. |
| -r | Reverse the order. |
| -k N | Sort by field (column) N. |
| -t SEP | Set the field separator (e.g. -t ','). |
| -u | Output only unique lines (remove duplicates). |
| -h | Human-numeric sort (understands 2K, 1M, 3G). |
| -f | Ignore case. |
| -o FILE | Write the result to a file (can be the input). |
Practical Examples
Real sort commands you can run today:
# Sort lines alphabetically
sort names.txt
# Sort numbers correctly
sort -n scores.txt
# Reverse (largest/last first)
sort -nr scores.txt
# Sort a CSV by the 3rd column
sort -t ',' -k3 -n data.csv
# Sort and remove duplicates
sort -u list.txt
# Sort human-readable sizes (du output)
du -h | sort -h
# Find the top 5 most common lines
sort file | uniq -c | sort -nr | head -5Tips and Best Practices
- Use
-nwhenever you sort numbers — alphabetic sort puts 10 before 2, which is rarely what you want. sort | uniq -c | sort -nris the classic recipe for a frequency-ranked list.sort -hunderstands human sizes, sodu -h | sort -hranks directories by real size.
Final Thoughts
sort is the ordering engine of the shell, equally at home alphabetising names, ranking numbers, and organising columns. Master -n, -r, and -k, and pair it with uniq for de-duplication and frequency counts. It turns unordered output into something you can actually read and analyse.
FAQ: sort Command in Linux
How do I sort numbers with sort?+
Use the -n flag: sort -n file sorts numerically so 2 comes before 10. Without -n, sort compares text and would place 10 before 2.
How do I sort in reverse order?+
Add -r: sort -r reverses alphabetical order and sort -nr reverses numeric order, putting the largest values first.
How do I sort by a specific column?+
Use -k for the field and -t for the separator: sort -t ',' -k3 -n data.csv sorts a CSV by the third column numerically.
How do I remove duplicate lines while sorting?+
Use -u: sort -u file outputs each unique line once. Alternatively, pipe sorted output into uniq.
How do I find the most frequent lines?+
Use the recipe sort file | uniq -c | sort -nr | head, which counts each line, then sorts by count descending to show the most common first.
Need help with Linux servers or infrastructure?
Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.
Hire Me for Support