🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

sort Command

Beginner Text Processing man(1)

Sort lines of text files

👁 8 views 📅 Updated: Mar 15, 2026
SYNTAX
sort [OPTION]... [FILE]...

What Does sort Do?

The sort command arranges lines of text in a specified order. It supports alphabetical, numerical, reverse, and custom sorting. sort is one of the fundamental Unix text processing tools, often combined with uniq, head, and other commands in pipelines.

sort can handle multiple sort keys, allowing you to sort by different columns with different criteria. It supports human-readable number sorting (1K, 2M, 3G), version number sorting, and month sorting. It can also remove duplicates while sorting.

For large files, sort automatically uses temporary files and can be configured for parallel sorting on multi-core systems. It is stable by default on most Linux systems, meaning equal elements maintain their original order.

Options & Flags

OptionDescriptionExample
-n Sort numerically instead of alphabetically sort -n numbers.txt
-r Reverse the sort order sort -rn scores.txt
-k Sort by specific field (column) sort -k2 -n data.txt
-t Set field delimiter sort -t',' -k3 -n data.csv
-u Remove duplicate lines (unique) sort -u names.txt
-h Sort human-readable numbers (1K, 2M, 3G) du -sh * | sort -h
-f Case-insensitive sort sort -f names.txt
-V Version number sort sort -V versions.txt
-o Write result to file (can be the same file) sort -o sorted.txt unsorted.txt

Practical Examples

#1 Sort alphabetically

Sorts lines in alphabetical order (default behavior).
$ sort names.txt

#2 Sort numerically (largest first)

Sorts numbers in descending order. -n treats values as numbers, -r reverses.
$ sort -rn scores.txt

#3 Sort by second column

Sorts CSV by the second column numerically using comma as delimiter.
$ sort -t',' -k2 -n employees.csv

#4 Find largest files

Sorts disk usage by human-readable size (G > M > K) in descending order.
$ du -sh /var/log/* | sort -rh
Output: 2.1G\t/var/log/journal\n500M\t/var/log/syslog

#5 Sort and remove duplicates

Sorts and removes duplicate lines in one step.
$ sort -u emails.txt

#6 Sort by multiple keys

Sorts /etc/passwd by GID (4th field) numerically, then by username alphabetically.
$ sort -t':' -k4 -n -k1 /etc/passwd

#7 Sort IP addresses

Sorts IP addresses correctly by treating each octet as a number.
$ sort -t. -k1,1n -k2,2n -k3,3n -k4,4n ips.txt

Tips & Best Practices

Sort in-place: Use -o flag to write back to the same file: sort -o file.txt file.txt. This is safe and atomic.
LC_ALL affects sorting: Locale settings affect sort order. For consistent byte-level sorting across systems, use: LC_ALL=C sort file.txt
Numeric vs alphabetic: Without -n, sort treats numbers as strings: 9 > 80 > 700. Always use -n for numerical data.

Frequently Asked Questions

How do I sort a file numerically?
Use sort -n filename. For descending order: sort -rn filename. For human-readable sizes: sort -h.
How do I sort by a specific column?
Use -k to specify the column: sort -k2 file.txt sorts by the 2nd column. Use -t to set a delimiter: sort -t',' -k3 file.csv.
What is the difference between sort -u and sort | uniq?
sort -u combines sorting and deduplication in one pass, making it more efficient. sort | uniq does the same in two separate commands.

Master Linux with Professional eBooks

Curated IT eBooks covering Linux, DevOps, Cloud, and more

Browse Books →