Skip to content

sort Command — Sorting Text in Linux

The sort command 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 like grep, cut, uniq, and awk to analyze logs, reports, and structured data.


Learning Path

Linux Mastery → Module 3: Text Processing → Lesson 4

Difficulty: Beginner → Intermediate

Reading Time: 30 Minutes

Course Progress

Course: Linux Mastery

Module: Text Processing

Lesson: 4 of 18


What You'll Learn

After completing this lesson, you'll be able to:

  • Understand the sort command
  • 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.

Charlie

Alice

David

Bob

Instead of manually rearranging the data:

sort employees.txt

Output:

Alice

Bob

Charlie

David

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:

sort [OPTIONS] FILE

Sample File

Create a sample file.

cat > employees.txt

Contents:

Charlie

Alice

David

Bob

Press:

Ctrl + D

Alphabetical Sorting

sort employees.txt

Output:

Alice

Bob

Charlie

David

Reverse Sorting

Use the -r option.

sort -r employees.txt

Output:

David

Charlie

Bob

Alice

Numeric Sorting

Create:

cat > numbers.txt

Contents:

25

8

100

50

10

Default sort:

sort numbers.txt

Output:

10

100

25

50

8

Numeric sort:

sort -n numbers.txt

Output:

8

10

25

50

100

Sort Unique Values

Remove duplicate entries.

Create:

Alice

Bob

Alice

David

Bob

Command:

sort -u employees.txt

Output:

Alice

Bob

David

Ignore Case

Create:

apple

Banana

APPLE

banana

Command:

sort -f fruits.txt

The -f option ignores case while sorting.


Sort by Month

Useful for log files.

Create:

Jan

Mar

Feb

Dec

Command:

sort -M months.txt

Output:

Jan

Feb

Mar

Dec

Sort Human-Readable Sizes

Create:

5K

100M

2G

500K

Command:

sort -h sizes.txt

Output:

5K

500K

100M

2G

Sort CSV Files by Column

Create:

cat > employees.csv

Contents:

Alice,85000

Bob,60000

Charlie,90000

David,75000

Sort by salary.

sort -t "," -k2 -n employees.csv

Output:

Bob,60000

David,75000

Alice,85000

Charlie,90000

Explanation:

  • -t "," → Delimiter
  • -k2 → Second column
  • -n → Numeric sort

Remove Duplicates with uniq

sort employees.txt | uniq

Count duplicates.

sort employees.txt | uniq -c

Sort Command Output

Sort users.

cut -d ":" -f1 /etc/passwd | sort

Sort processes.

ps -ef | sort

Sort mounted filesystems.

mount | sort

Combining with Pipes

Count unique departments.

cut -d "," -f2 employees.csv | sort | uniq | wc -l

Sort running processes.

ps -ef | grep nginx | sort

Sort log entries.

grep ERROR application.log | sort

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.

cut -d ":" -f1 /etc/passwd | sort

Sort log entries.

grep ERROR app.log | sort

Sort Docker images.

docker images | sort

Sort Kubernetes namespaces.

kubectl get namespaces | sort

Sort disk usage.

du -sh * | sort -h

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:

cat > students.txt

Contents:

Rahul

Anita

Vijay

Deepa

Task 2

Sort alphabetically.

sort students.txt

Task 3

Reverse sort.

sort -r students.txt

Task 4

Create numbers.

cat > marks.txt

Contents:

95

80

100

70

85

Task 5

Sort numerically.

sort -n marks.txt

Task 6

Remove duplicates.

sort -u students.txt

Task 7

Sort usernames.

cut -d ":" -f1 /etc/passwd | sort

Task 8

Sort salaries.

sort -t "," -k2 -n employees.csv

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.

server01 500M

server02 2G

server03 250M

server04 1G

Tasks:

  1. Sort servers by disk usage.
  2. Display the largest server.
  3. Remove duplicate entries.
  4. 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:

employees.csv
Alice,85000

Bob,60000

Charlie,90000

David,75000

Bob,60000

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 -n for numbers instead of default sorting.
  • Use sort -h for file sizes.
  • Always sort before using uniq.
  • Use field-based sorting for CSV files.
  • Combine sort with grep, cut, and awk for powerful text processing.

Common Mistakes

❌ Sorting numbers without -n.

✅ Incorrect:

sort numbers.txt

Correct:

sort -n numbers.txt

❌ Using uniq on unsorted data.

✅ Incorrect:

uniq file.txt

Correct:

sort file.txt | uniq

❌ Forgetting the delimiter when sorting CSV files.

✅ Correct:

sort -t "," -k2 employees.csv

Interview Questions

Beginner

  1. What does the sort command do?
  2. What is the purpose of -n?
  3. What does -r do?
  4. How do you remove duplicate entries?

Intermediate

  1. Why should data be sorted before using uniq?
  2. Explain sort -k.
  3. What does sort -h do?
  4. How do you sort CSV data by a specific column?

Architect Level

  1. How would you sort a report containing millions of records efficiently?
  2. Why is sort commonly used before uniq in shell pipelines?
  3. How can sort improve 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

  • sort arranges text in ascending order by default.
  • Use -n for numeric values.
  • Use -r for reverse order.
  • Use -u to remove duplicates.
  • Use -k and -t to 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 uniq with sort
  • Production log and report analysis