🗂️ Command: sort (Data Organizer)
The sort command does exactly what it says: it reorders lines of text alphabetically or numerically. While it sounds simple, it is a key component of the Linux "pipeline" philosophy—taking messy output and making it structured.
1. The "Why"
For a developer and author, sort helps you make sense of large amounts of information.
- Log Analysis: Sort your journalctl or Supabase logs by timestamp or error severity to find patterns.
- E-book Management: Organize a list of references, citations, or glossary terms .
- Dependency Management: Sort a list of Java imports or Linux packages to find duplicates or organize your configuration.
- Disk Cleanup: Find the largest files in your system by sorting the output of
du(disk usage).
2. Basic Syntax
sort [OPTIONS] [FILENAME]
By default, sort organizes lines alphabetically (A-Z) based on the start of the line.
3. Essential Flags for Power Users
| Flag | Purpose | Example |
|---|---|---|
-n |
Numerical | Sorts by number value (1, 2, 10) instead of string (1, 10, 2). |
-r |
Reverse | Sorts in descending order (Z-A or 10-1). |
-u |
Unique | Removes duplicate lines while sorting. |
-k [x] |
Key/Column | Sorts based on a specific column (e.g., the 2nd word in a line). |
-h |
Human-readable | Correctly sorts file sizes like "2K", "1G", "500M". |
4. Practical Examples for Your Workflow
A. Sorting a List of Project Ideas
If you have a file ideas.txt and want them alphabetically:
sort ideas.txt
B. Finding the Top 5 Largest Files
Combine du with sort to see what's eating your SSD space:
du -sh * | sort -rh | head -n 5
-h: Handles the "G", "M", and "K" units correctly.-r: Puts the largest files at the top.
C. Sorting by a Specific Column
If you have a CSV-style list of users or data from your documentation viewer backend:
# Sort by the 2nd column (e.g., Last Name)
sort -k 2 data.txt
D. Cleaning up Duplicates
If you’ve accidentally pasted the same Java library dependencies multiple times:
sort -u dependencies.list -o dependencies.list
The -o flag saves the sorted result back into the original file.
5. The "Unique" Duo: sort | uniq
While sort -u exists, you will often see sort paired with the uniq command. This is powerful for counting occurrences.
Example: Who is trying to log into your system?
journalctl | grep "Failed password" | awk '{print $11}' | sort | uniq -c | sort -nr
This counts how many times each IP address failed to log in, sorted by the most frequent offenders.
6. Pro-Tips
- Version Sorting (
-V): Great for sorting software versions (e.g.,v1.2,v1.10,v1.9). A standard sort would putv1.10beforev1.9, but-Vhandles it correctly. - Arch Linux Context: Use
pacman -Qq | sortto get a perfectly alphabetized list of every package installed on your system. - Randomize (
-R): If you need a random order (perhaps for a test script),sort -Rwill shuffle the lines for you.
7. Summary Reference
| Goal | Command |
|---|---|
| Alphabetical Sort | sort file.txt |
| Numeric Sort | sort -n file.txt |
| Reverse Numeric | sort -nr file.txt |
| Sort by 3rd Column | sort -k 3 file.txt |
| Unique lines only | sort -u file.txt |