Mastering the Sort Command in Linux: A Comprehensive Guide
The sort command in Linux is a essential command-line utility used to reorder lines in text files or streams. By default, sort processes data lexicographically (alphabetically, character-by-character). However, it provides options to handle numerical values, reverse ordering, field/column-based sorting, human-readable file sizes, and duplicate removal.
1. Default Lexicographical Sorting
When invoked without any flags, sort reads input line-by-line and orders the content alphabetically based on ASCII values.
Basic Syntax
sort [OPTION]... [FILE]...
Example Walkthrough
Consider a file named foods.txt with unordered items:
grapes
cheese
apple
zucchini
Running the default sort command:
sort foods.txt
Output:
apple
cheese
grapes
zucchini
Note: As indicated in the manual page (
man sort),sortcan accept multiple space-separated files directly or read target file paths from a text list using the-f/--files-fromoption.
2. Reversing Sort Order (-r)
To invert the standard sort sequence—arranging text from Z to A or numbers from highest to lowest—use the -r (reverse) flag.
Command Execution
sort -r foods.txt
Output:
zucchini
grapes
cheese
apple
3. String vs. Numeric Sorting (-n)
A common pitfall occurs when sorting numbers using default lexicographical mode. Because string comparison evaluates input character-by-character from left to right, numerical sequences produce counter-intuitive results.
The Naive Numeric Problem
Consider a file named numbers.txt:
09
10
2
3
Executing a standard sort:
sort numbers.txt
Output (Incorrect Order):
09
10
2
3
Why this fails: Default string comparison compares the first character of each line.
'0'(in09) comes before'1'(in10), and'1'comes before'2'or'3'. It treats the data as plain text strings rather than arithmetic quantities.
The Solution: Numerical Evaluation (-n)
To force Linux to evaluate lines based on integer numerical values, append the -n flag:
sort -n numbers.txt
Output (Correct Numerical Order):
2
3
09
10
To sort numbers in descending numerical order, combine the numeric and reverse flags:
sort -nr numbers.txt
4. Capturing Output for Sorting Workflows
Before mastering pipeline redirection (|), intermediate terminal outputs must be saved to a temporary text file using output redirection (>) to perform multi-stage sorting operations.
Real-World Use Case: Sorting Directory Contents
To sort directory contents generated by ls -lh:
- Redirect the command output into a storage file:
BASH
ls -lh > files.txt - Verify the stored contents:
BASH
cat files.txt
Running a basic sort files.txt at this point will fail to order by size because it defaults to evaluating column 1 (file permission strings such as -rw-r--r-- or drwxr-xr-x).
5. Column-Based Field Sorting (-k)
Directory listings and CSV-like data consist of multiple whitespace-separated columns. The -k (key) flag allows targeting a specific column field for sorting.
Analyzing Field Positions in ls -lh
-rw-r--r-- 1 user group 148M Oct 24 10:00 bigfile.zip
^ ^ ^ ^ ^
Col 1 C2 C3 Col 4 Column 5 (File Size)
- Column 1: File permissions (
-rw-r--r--) - Column 2: Hard link count (
1) - Column 3: Owner (
user) - Column 4: Group (
group) - Column 5: File size (
148M)
To sort specifically by the 5th column, specify -k 5:
sort -k 5 files.txt
6. Sorting Human-Readable File Sizes (-h)
Standard numerical sorting (-n) breaks when file sizes contain unit suffixes like K (Kilobytes), M (Megabytes), or G (Gigabytes). The system requires the human-numeric sort flag (-h) to parse these suffixes correctly.
Sorting Files by Size (Ascending & Descending)
To sort by Column 5 taking human-readable units (148M, 2K, 500B) into account:
sort -k 5h files.txt
To find the largest files at the top of the list, append r to reverse the output:
sort -k 5hr files.txt
7. Deduplicating Output (-u)
When processing text containing duplicate rows, the -u (unique) flag sorts the file while discarding duplicate entries.
Example Walkthrough
Consider fruits.txt:
apple
banana
kiwi
apple
Sorting with deduplication:
sort -u fruits.txt
Output:
apple
banana
kiwi
8. Complete Execution Flow Walkthrough
The following workflow illustrates how file size extraction, column targeting, human unit parsing, and reverse sorting operate sequentially:
9. Command Flag Summary Matrix
Summary and Key Takeaways
- Default Lexicographical Behavior: By default,
sortevaluates lines character-by-character from left to right based on ASCII values. - Character Sorting Pitfalls: Plain
sortfails on numerical lists (e.g., placing10before2). Always use-nfor numerical data. - Reversing Output: Pass
-rto flip the output order from ascending (A-Z, lowest-to-highest) to descending (Z-A, highest-to-lowest). - Column-Level Precision: Use
-k [COLUMN_NUMBER]to isolate specific fields in whitespace-delimited outputs (such asls -lcolumns). - Handling Storage Units: Use
-hwhen sorting file sizes containing human-readable metric suffixes (K,M,G). - Combining Options: Flags can be concatenated for specific criteria—such as
-k 5hrto sort by the 5th column numerically with unit awareness in reverse order. - In-Line Deduplication: Use
-uto automatically filter out duplicate lines while sorting.