How to Sort Lines of Text in Linux (sort)
sort orders the lines of a file or piped input alphabetically or numerically.
Published October 2, 2026
sort orders lines of text alphabetically by default. Use -n for numeric, -r for reverse, -k to sort by a column and -u to drop duplicates.
How it works
Locale affects ordering; set LC_ALL=C for byte order. Numeric sort avoids the '10 before 2' problem.
sort | uniq -c | sort -rn is the classic frequency-count idiom.
Try it
sort names.txt
Ada
Bob
Cy
sort -n numbers.txt
Practical examples
Numeric and reverse
sort -nr scores.txtBy second column
sort -k2,2 -t, data.csvUnique sorted
sort -u list.txtFrequency count
cut -d' ' -f1 access.log | sort | uniq -c | sort -rn | headCommon mistakes
- Alphabetic sort on numbers (10 before 2).
- Forgetting uniq needs sorted input.
- Locale differences between machines.
Key takeaways
- -n numeric, -r reverse, -k column.
- Pair with uniq.
- Set LC_ALL=C for consistency.
FAQ
Sort a human-readable size?
sort -h.
Stable sort?
Use -s.