Linux sort Guide - ryzendew/Linux-Tips-and-Tricks GitHub Wiki
Linux sort Guide
Complete beginner-friendly guide to sort on Linux, covering Arch Linux, CachyOS, and other distributions including sorting text, data organization, and file sorting.
Table of Contents
Understanding sort
What is sort?
sort sorts lines of text.
Uses:
- Sort text: Organize text files
- Data organization: Sort data
- Remove duplicates: Unique lines
- Merge files: Combine sorted files
Why it matters:
- Organization: Organize data
- Data processing: Process text files
- Scripting: Useful in scripts
sort Basics
Basic Sort
Sort file:
# Sort file
sort file.txt
# Alphabetical order
Sort and Save
Save sorted:
# Sort and save
sort file.txt > sorted.txt
# Or in place (with sponge)
sort file.txt | sponge file.txt
Sort Options
Reverse Sort
Descending:
# Reverse sort
sort -r file.txt
# Descending order
Numeric Sort
Numbers:
# Numeric sort
sort -n file.txt
# Sorts numbers correctly
Unique Lines
Remove duplicates:
# Unique lines only
sort -u file.txt
# Or
sort file.txt | uniq
Advanced Sorting
Sort by Field
Field sorting:
# Sort by field
sort -t',' -k2 file.csv
# -t: Delimiter
# -k: Key (field)
Multiple Keys
Complex sorting:
# Sort by multiple fields
sort -t',' -k1,1 -k2,2n file.csv
# Primary: field 1
# Secondary: field 2 (numeric)
Troubleshooting
sort Errors
Check file:
# Verify file exists
ls -la file.txt
# Check encoding
file file.txt
Summary
This guide covered sort usage, text sorting, and data organization for Arch Linux, CachyOS, and other distributions.
Next Steps
- sed and awk Guide - Text processing
- cut Guide - Field extraction
- uniq Guide - Remove duplicates
- sort Documentation:
man sort
This guide covers Arch Linux, CachyOS, and other Linux distributions. For distribution-specific details, refer to your distribution's documentation.