Skip to main content

Syntax

sort [OPTIONS] [FILE...]

Description

The sort command arranges lines of text in order. By default, it performs ascending lexicographic (alphabetical) sorting. It’s essential for organizing data, finding patterns, and preparing input for commands like uniq. Nash’s sort operates entirely in-memory and supports both regular and reverse sorting, with an option to remove duplicates during the sort process.

Options

-r
flag
Reverse the sorting order. Sort from Z to A, or highest to lowest.
-u
flag
Unique mode. Output only unique lines, removing adjacent duplicates after sorting.Equivalent to sort | uniq.

Examples

Basic alphabetical sort

echo -e "cherry\napple\nbanana" | sort
apple
banana
cherry

Reverse sort

echo -e "apple\nbanana\ncherry" | sort -r
cherry
banana
apple

Sort with duplicates

echo -e "dog\ncat\ndog\napple\ncat" | sort
apple
cat
cat
dog
dog

Unique sort

echo -e "dog\ncat\ndog\napple\ncat" | sort -u
apple
cat
dog

Sort numbers (lexicographic)

echo -e "10\n2\n1\n20" | sort
1
10
2
20
Nash’s sort performs lexicographic (string) sorting. Numbers are sorted as text, so “10” comes before “2” because “1” < “2” alphabetically.

Sort file contents

echo -e "zebra\napple\nmango" > fruits.txt
sort fruits.txt
apple
mango
zebra

Case sensitivity

echo -e "Zebra\napple\nMango" | sort
Mango
Zebra
apple
Uppercase letters come before lowercase in ASCII order.

Pipeline Examples

Sort then filter duplicates

cat access.log | cut -d' ' -f1 | sort | uniq
Extracts IP addresses, sorts them, and shows unique values.

Count occurrences

cat errors.log | sort | uniq -c | sort -r
  1. Sort error messages
  2. Count each unique message
  3. Sort by count (descending)

Find common items

sort file1.txt file2.txt | uniq -d
Finds lines that appear in both files.

Top N items

cat data.txt | sort | uniq -c | sort -r | head -10
Shows the 10 most frequent items.

Reverse unique sort

echo -e "a\nc\nb\na\nb" | sort -ru
c
b
a

Practical Use Cases

Organize list of names

cat attendees.txt | sort > attendees-sorted.txt

Deduplicate email list

cat emails.txt | sort -u > unique-emails.txt

Find duplicate entries

sort inventory.txt | uniq -d
Shows only items that appear more than once.

Prepare for uniq

cat logs.txt | sort | uniq -c
uniq only removes adjacent duplicates, so sort first.

Alphabetize configuration

cat .env | grep -v "^#" | sort
Sort environment variables, skipping comments.

Find all unique words

cat document.txt | tr ' ' '\n' | sort -u
Splits text into words and lists unique ones.

Merge and sort files

sort file1.txt file2.txt file3.txt > combined-sorted.txt

Advanced Examples

Sort and reverse

echo -e "3\n1\n2" | sort -r
3
2
1

Extract and sort unique fields

cat data.csv | cut -d, -f2 | sort -u
Extracts second column and sorts unique values.

Sort by multiple criteria (simulated)

cat data.txt | sort | sort -s -k2
Note: Nash’s sort doesn’t support field keys, but you can preprocess with cut.

Remove blank lines and sort

cat file.txt | grep -v "^$" | sort

Sort then process

ls | sort -r | head -5
Shows the last 5 files alphabetically.

Comparing Sort Options

echo -e "c\na\nb" | sort
Output:
a
b
c

Tips

Always sort before uniq

uniq only removes adjacent duplicates:
# Wrong - won't remove all duplicates
echo -e "a\nb\na" | uniq

# Right - sort first
echo -e "a\nb\na" | sort | uniq

Use -u for efficiency

Instead of:
sort file.txt | uniq
Use:
sort -u file.txt

Stable sort

Nash’s sort preserves the relative order of equal elements (stable sort).

Empty lines

Empty lines sort to the beginning:
echo -e "b\n\na" | sort  # Empty line appears first

Performance

Nash’s sort loads all input into memory, sorts it, then outputs the result. For very large datasets, consider processing in chunks or using the -u flag to reduce output size.
  • uniq - Filter duplicate lines
  • cut - Extract fields for sorting
  • grep - Filter before sorting
  • head - View top N sorted items
  • tail - View bottom N sorted items
  • wc - Count sorted output

Build docs developers (and LLMs) love