sort Command — Sorting Text in Linux¶
The
sortcommand is used to arrange lines of text in alphabetical, numerical, or custom order. It is one of the most commonly used Linux text-processing utilities and is frequently combined with commands likegrep,cut,uniq, andawkto analyze logs, reports, and structured data.
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Understand the
sortcommand - Sort text alphabetically
- Sort numbers
- Sort in reverse order
- Sort using specific columns
- Remove duplicate entries
- Perform case-insensitive sorting
- Analyze real-world log and report files
Prerequisites¶
Before starting this lesson, complete:
- Module 1 – Linux Fundamentals
- Module 2 – Linux Command Line Essentials
- Module 3 Lessons 1–3
Why Learn sort?¶
Imagine you receive a report with thousands of employee records.
Instead of manually rearranging the data:
Output:
Sorting data is a common task in Linux administration, DevOps, and data analysis.
What is sort?¶
The sort command reads lines from one or more files and arranges them in a specified order.
Syntax:
Sample File¶
Create a sample file.
Contents:
Press:
Alphabetical Sorting¶
Output:
Reverse Sorting¶
Use the -r option.
Output:
Numeric Sorting¶
Create:
Contents:
Default sort:
Output:
Numeric sort:
Output:
Sort Unique Values¶
Remove duplicate entries.
Create:
Command:
Output:
Ignore Case¶
Create:
Command:
The -f option ignores case while sorting.
Sort by Month¶
Useful for log files.
Create:
Command:
Output:
Sort Human-Readable Sizes¶
Create:
Command:
Output:
Sort CSV Files by Column¶
Create:
Contents:
Sort by salary.
Output:
Explanation:
-t ","→ Delimiter-k2→ Second column-n→ Numeric sort
Remove Duplicates with uniq¶
Count duplicates.
Sort Command Output¶
Sort users.
Sort processes.
Sort mounted filesystems.
Combining with Pipes¶
Count unique departments.
Sort running processes.
Sort log entries.
Common sort Options¶
| Option | Description |
|---|---|
-r | Reverse order |
-n | Numeric sort |
-u | Unique values |
-f | Ignore case |
-M | Month names |
-h | Human-readable sizes |
-t | Field delimiter |
-k | Sort by column |
Real Production Examples¶
Sort system users.
Sort log entries.
Sort Docker images.
Sort Kubernetes namespaces.
Sort disk usage.
Production Perspective¶
The sort command is commonly used to:
- Organize reports
- Sort log entries
- Process CSV files
- Prepare data for
uniq - Analyze command output
- Generate readable reports
Nearly every Linux engineer uses sort as part of daily workflows.
Hands-on Lab¶
Task 1¶
Create:
Contents:
Task 2¶
Sort alphabetically.
Task 3¶
Reverse sort.
Task 4¶
Create numbers.
Contents:
Task 5¶
Sort numerically.
Task 6¶
Remove duplicates.
Task 7¶
Sort usernames.
Task 8¶
Sort salaries.
Command Deep Dive¶
| Command | Purpose | Production Example |
|---|---|---|
sort file | Alphabetical sort | Reports |
sort -n | Numeric sort | CPU usage |
sort -r | Reverse sort | Latest entries |
sort -u | Unique values | User lists |
sort -k2 | Sort by column | CSV reports |
sort -h | Human-readable sizes | Disk usage |
Production Troubleshooting Scenario¶
Scenario
You receive a report showing disk usage across multiple servers.
Tasks:
- Sort servers by disk usage.
- Display the largest server.
- Remove duplicate entries.
- Generate a sorted report.
Solutions:
sort -k2 -h servers.txt
sort -k2 -h servers.txt | tail -1
sort -u servers.txt
sort servers.txt > sorted-report.txt
Mini Challenge¶
Create:
Perform the following:
- Sort alphabetically.
- Sort by salary.
- Sort in reverse order.
- Remove duplicate entries.
- Count unique employees.
- Display the employee with the highest salary.
Best Practices¶
- Use
sort -nfor numbers instead of default sorting. - Use
sort -hfor file sizes. - Always sort before using
uniq. - Use field-based sorting for CSV files.
- Combine
sortwithgrep,cut, andawkfor powerful text processing.
Common Mistakes¶
❌ Sorting numbers without -n.
✅ Incorrect:
Correct:
❌ Using uniq on unsorted data.
✅ Incorrect:
Correct:
❌ Forgetting the delimiter when sorting CSV files.
✅ Correct:
Interview Questions¶
Beginner¶
- What does the
sortcommand do? - What is the purpose of
-n? - What does
-rdo? - How do you remove duplicate entries?
Intermediate¶
- Why should data be sorted before using
uniq? - Explain
sort -k. - What does
sort -hdo? - How do you sort CSV data by a specific column?
Architect Level¶
- How would you sort a report containing millions of records efficiently?
- Why is
sortcommonly used beforeuniqin shell pipelines? - How can
sortimprove automation and reporting in DevOps workflows?
Summary¶
In this lesson, you learned:
- Alphabetical sorting
- Numeric sorting
- Reverse sorting
- Field-based sorting
- Case-insensitive sorting
- Human-readable size sorting
- Removing duplicates
- Real-world production use cases
The sort command is one of the most essential Linux text-processing tools. It is frequently combined with cut, grep, uniq, awk, and sed to organize and analyze data efficiently.
Key Takeaways¶
sortarranges text in ascending order by default.- Use
-nfor numeric values. - Use
-rfor reverse order. - Use
-uto remove duplicates. - Use
-kand-tto sort by specific columns. - Always sort data before using
uniq.
What's Next?¶
uniq Command — Removing Duplicate Lines in Linux
In the next lesson, you'll learn:
- Removing duplicate lines
- Counting duplicate entries
- Displaying unique and repeated lines
- Combining
uniqwithsort - Production log and report analysis