sort Command in Linux: Sort Lines of Text

sort is a command-line utility that sorts lines of text from files or standard input and writes the result to standard output. By default, sort arranges lines alphabetically, but it supports numeric sorting, reverse order, sorting by specific fields, and more.
This guide explains how to use the sort command with practical examples.
sort Command Syntax
The syntax for the sort command is as follows:
sort [OPTIONS] [FILE]...If no file is specified, sort reads from standard input. When multiple files are given, their contents are merged and sorted together.
Sorting Lines Alphabetically
By default, sort sorts lines in ascending alphabetical order. To sort a file, pass the file name as an argument:
sort file.txtFor example, given a file with the following content:
banana
apple
cherry
dateRunning sort produces:
apple
banana
cherry
dateThe original file is not modified. sort writes the sorted output to standard output. To save the result to a file, use output redirection:
sort file.txt > sorted.txtSorting in Reverse Order
To sort in descending order, use the -r (--reverse) option:
sort -r file.txtdate
cherry
banana
appleSorting Numerically
Alphabetical sorting does not work correctly for numbers. For example, 10 would sort before 2 because 1 comes before 2 alphabetically. Use the -n (--numeric-sort) option to sort numerically:
sort -n numbers.txtGiven the file:
10
2
30
5The output is:
2
5
10
30To sort numerically in reverse (largest first), combine -n and -r:
sort -nr numbers.txt30
10
5
2Sorting by a Specific Column
By default, sort uses the entire line as the sort key. To sort by a specific field (column), use the -k (--key) option followed by the field number.
Fields are separated by whitespace by default. For example, to sort the following file by the second column (file size):
report.pdf 2500
notes.txt 80
archive.tar 15000
readme.md 200sort -k2 -n files.txtnotes.txt 80
readme.md 200
report.pdf 2500
archive.tar 15000To use a different field separator, specify it with the -t option. For example, to sort /etc/passwd by the third field (UID), using : as the separator:
sort -t: -k3 -n /etc/passwdRemoving Duplicate Lines
To output only unique lines (removing adjacent duplicates after sorting), use the -u (--unique) option:
sort -u file.txtGiven a file with repeated entries:
apple
banana
apple
cherry
bananaapple
banana
cherryThis is equivalent to running sort file.txt | uniq.
Ignoring Case
By default, sort is case-sensitive. Uppercase letters sort before lowercase. To sort case-insensitively, use the -f (--ignore-case) option:
sort -f file.txtChecking if a File Is Already Sorted
To verify that a file is already sorted, use the -c (--check) option. It exits silently if the file is sorted, or prints an error and exits with a non-zero status if it is not:
sort -c file.txtsort: file.txt:2: disorder: bananaThis is useful in scripts to validate input before processing.
Sorting Human-Readable Sizes
When working with output from commands like du, sizes are expressed in human-readable form such as 1K, 2M, or 3G. Standard numeric sort does not handle these correctly. Use the -h (--human-numeric-sort) option:
du -sh /var/* | sort -h4.0K /var/backups
16K /var/cache
1.2M /var/log
3.4G /var/libCombining sort with Other Commands
sort is commonly used in pipelines with other commands.
To count and rank the most frequent words in a file, combine grep
, sort, and uniq:
grep -Eo '[[:alnum:]_]+' file.txt | sort | uniq -c | sort -rnTo display the ten largest files in a directory, combine sort with head
:
du -sh /var/* | sort -rh | head -10To extract and sort a specific field from a CSV using cut
:
cut -d',' -f2 data.csv | sortQuick Reference
| Option | Description |
|---|---|
sort file.txt | Sort alphabetically (ascending) |
sort -r file.txt | Sort in reverse order |
sort -n file.txt | Sort numerically |
sort -nr file.txt | Sort numerically, largest first |
sort -k2 file.txt | Sort by the second field |
sort -t: -k3 file.txt | Sort by field 3 using : as separator |
sort -u file.txt | Sort and remove duplicate lines |
sort -f file.txt | Sort case-insensitively |
sort -h file.txt | Sort human-readable sizes (1K, 2M, 3G) |
sort -c file.txt | Check if file is already sorted |
For a printable quick reference, see the sort cheatsheet .
Troubleshooting
Numbers sort in wrong order
You are using alphabetical sort on numeric data. Add the -n option to sort numerically: sort -n file.txt.
Human-readable sizes sort incorrectly
Sizes like 1K, 2M, and 3G require -h (--human-numeric-sort). Standard -n only handles plain integers.
Uppercase entries appear before lowercasesort is case-sensitive by default. Use -f to sort case-insensitively, or use LC_ALL=C sort to enforce byte-order sorting.
Output looks correct on screen but differs from file
Output redirection with > truncates the file before reading. Never redirect to the same file you are sorting: sort file.txt > file.txt will empty the file. Use a temporary file or the sponge utility from moreutils.
FAQ
Does sort modify the original file?
No. sort writes to standard output and never modifies the input file. Use sort file.txt > sorted.txt or sort -o file.txt file.txt to save the output.
How do I sort a file and save it in place?
Use the -o option: sort -o file.txt file.txt. Unlike > file.txt, the -o option is safe to use with the same input and output file.
How do I sort by the last column?
Use a negative field number with the -k option: sort -k-1 sorts by the last field. Alternatively, use awk to reorder columns before sorting.
What is the difference between sort -u and sort | uniq?sort -u is slightly more efficient as it removes duplicates during sorting. sort | uniq is more flexible because uniq supports options like -c (count occurrences) and -d (show only duplicates).
How do I sort lines randomly?
Use sort -R (random sort) or the shuf command: shuf file.txt. Both produce a random permutation of the input lines.
Conclusion
The sort command is a versatile tool for ordering lines of text in files or command output. It is most powerful when combined with other commands like grep
, cut
, and head
in pipelines.
If you have any questions, feel free to leave a comment below.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page