Quick take: The sort command orders lines of text. Use -n for numeric sorting, -r to reverse, -k to sort by a specific column, and -u to remove duplicates while sorting.

Introduction

The sort command arranges lines of input into order — alphabetical by default, or numeric, reverse, and by column with the right flags. It is a pipeline staple, used to organise output, prepare data for uniq, and rank results.

This guide covers the common sort modes, sorting by a chosen field, and combining sort with other commands to produce ranked and de-duplicated output.

Syntax

The basic syntax of the sort command is:

sort [OPTIONS] [FILE...]

Common Options and Parameters

The most useful options and parameters for the sort command:

OptionDescription
-nSort numerically rather than alphabetically.
-rReverse the order.
-k NSort by field (column) N.
-t SEPSet the field separator (e.g. -t ',').
-uOutput only unique lines (remove duplicates).
-hHuman-numeric sort (understands 2K, 1M, 3G).
-fIgnore case.
-o FILEWrite the result to a file (can be the input).

Practical Examples

Real sort commands you can run today:

# Sort lines alphabetically
sort names.txt
# Sort numbers correctly
sort -n scores.txt
# Reverse (largest/last first)
sort -nr scores.txt
# Sort a CSV by the 3rd column
sort -t ',' -k3 -n data.csv
# Sort and remove duplicates
sort -u list.txt
# Sort human-readable sizes (du output)
du -h | sort -h
# Find the top 5 most common lines
sort file | uniq -c | sort -nr | head -5

Tips and Best Practices

  • Use -n whenever you sort numbers — alphabetic sort puts 10 before 2, which is rarely what you want.
  • sort | uniq -c | sort -nr is the classic recipe for a frequency-ranked list.
  • sort -h understands human sizes, so du -h | sort -h ranks directories by real size.

Final Thoughts

sort is the ordering engine of the shell, equally at home alphabetising names, ranking numbers, and organising columns. Master -n, -r, and -k, and pair it with uniq for de-duplication and frequency counts. It turns unordered output into something you can actually read and analyse.

FAQ: sort Command in Linux

How do I sort numbers with sort?+

Use the -n flag: sort -n file sorts numerically so 2 comes before 10. Without -n, sort compares text and would place 10 before 2.

How do I sort in reverse order?+

Add -r: sort -r reverses alphabetical order and sort -nr reverses numeric order, putting the largest values first.

How do I sort by a specific column?+

Use -k for the field and -t for the separator: sort -t ',' -k3 -n data.csv sorts a CSV by the third column numerically.

How do I remove duplicate lines while sorting?+

Use -u: sort -u file outputs each unique line once. Alternatively, pipe sorted output into uniq.

How do I find the most frequent lines?+

Use the recipe sort file | uniq -c | sort -nr | head, which counts each line, then sorts by count descending to show the most common first.

Need help with Linux servers or infrastructure?

Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.

Hire Me for Support