sort Command Guide
The sort command sorts lines of text files. Learn how to organize data alphabetically, numerically, and by specific fields.
6 min read•Last updated: 2024
Dai Aoki
CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm
Quick Reference
Basic
sort fileSort alphabeticallysort -r fileReverse ordersort -n fileNumeric sortOptions
-uUnique (remove dups)-fIgnore case-hHuman numeric (1K, 2M)Fields
-k 2Sort by field 2-t ":"Field delimiter-k 2,2nNumeric field 2Common
sort -u fileUnique sortedsort -rn fileDescending numericsort -o file fileSort in placeDownloadable Image Preview
Failed to generate preview
Basic Usage
By default, sort arranges lines alphabetically.
bash
sort file.txtCommon Options
sort Options
| -r | Reverse order (descending) |
| -n | Numeric sort |
| -h | Human-readable numbers (1K, 2M) |
| -k N | Sort by field N |
| -t CHAR | Use CHAR as field delimiter |
| -u | Remove duplicate lines |
| -f | Ignore case |
| -o FILE | Write output to FILE |
Sorting Order
bash
# Alphabetical (default)
sort names.txt
# Reverse alphabetical
sort -r names.txt
# Ignore case
sort -f names.txtNumeric Sorting
bash
# Alphabetical (wrong for numbers)
echo -e "10\n2\n1\n20" | sort
# Output: 1, 10, 2, 20
# Numeric (correct)
echo -e "10\n2\n1\n20" | sort -n
# Output: 1, 2, 10, 20
# Human-readable sizes
du -h | sort -h
# Properly sorts 1K, 2M, 3GWarning
Without
-n, numbers are sorted alphabetically (1, 10, 2, 20). Always use -n for numeric data.Sorting by Field
Use -k to sort by a specific column.
bash
# Sort by second field
sort -k2 data.txt
# Sort by third field numerically
sort -k3 -n data.txt
# Sort by second field, then by third
sort -k2,2 -k3,3n data.txtCustom field delimiter
bash
# CSV file (comma-separated)
sort -t',' -k2 data.csv
# Colon-separated (like /etc/passwd)
sort -t':' -k3 -n /etc/passwdRemoving Duplicates
bash
# Remove duplicate lines
sort -u file.txt
# Same as
sort file.txt | uniqPractical Examples
Sort processes by memory usage
bash
ps aux | sort -k4 -rn | head -10Find largest files
bash
du -ah | sort -rh | head -10Sort IP addresses
bash
sort -t'.' -k1,1n -k2,2n -k3,3n -k4,4n ips.txtSort log by timestamp
bash
# ISO format dates sort correctly alphabetically
sort access.logCount occurrences (with uniq)
bash
# Most common words
cat file.txt | tr ' ' '\n' | sort | uniq -c | sort -rn | head -10Sort and save to same file
bash
sort -o file.txt file.txtCheck if file is sorted
bash
sort -c file.txt
# Returns exit code 0 if sorted, 1 if notRandom shuffle
bash
sort -R file.txt
# Or use shuf
shuf file.txtStable Sort
Use -s for stable sorting (preserves original order for equal elements).
bash
sort -s -k1,1 file.txtVersion Sorting
bash
# Sort version numbers correctly
echo -e "1.2\n1.10\n1.1\n2.0" | sort -V
# Output: 1.1, 1.2, 1.10, 2.0Summary
sort is essential for organizing text data. Key takeaways:
- Use
sort -nfor numeric sorting - Use
sort -rfor reverse order - Use
sort -k Nto sort by field N - Use
sort -tto set field delimiter - Use
sort -uto remove duplicates - Use
sort -hfor human-readable sizes