sort Command in Linux: Syntax, Options & Practical Examples

sort reads lines from files or stdin and prints them in order. Use it for log lines, CSV columns, version strings, and pipeline output from ls, awk, or find — then pair with uniq to drop duplicates.

Published

Updated

Read time 9 min read

Reviewed byDeepak Prasad

sort Command in Linux: Syntax, Options & Practical Examples
About sort reads lines from files or stdin and prints them in order. Use it for log lines, CSV columns, version strings, and pipeline output from ls, awk, or find — then pair with uniq to drop duplicates.
Tested on Ubuntu 25.04 (Plucky Puffin); sort (uutils coreutils) 0.2.2; kernel 7.0.0-27-generic
Package coreutils (apt/deb) · coreutils (dnf/rpm)
Man page sort(1)
Privilege none
Distros

GNU coreutils on most Linux distros. Ubuntu 25.04 ships uutils coreutils as /usr/bin/sort — flags match this page when tested there.

Remove adjacent duplicates after sorting: uniq (often sort file | uniq).

Related guide

sort — quick reference

Basic sorting

Read lines from files or stdin and print them in order. With no file argument, sort reads standard input.

When to use Command
Sort lines alphabetically (default locale order) sort file.txt
Sort lines from a pipe command | sort
Reverse the sort order sort -r file.txt
Fold upper and lower case together sort -f file.txt
Stable sort — keep original order among equal keys sort -s file.txt
Output only the first line in each run of equal keys sort -u file.txt

Keys, fields, and delimiters

Sort by columns instead of the whole line. Fields default to whitespace-separated tokens unless you set -t.

When to use Command
Sort by the second field sort -k2 file.txt
Sort by field 2 numerically sort -k2,2n file.txt
Sort CSV by the second column (comma delimiter) sort -t',' -k2,2n file.csv
Ignore leading blanks when finding sort keys sort -b file.txt
Consider only letters, digits, and blanks sort -d file.txt
Ignore non-printing characters sort -i file.txt

Numeric and special sort modes

Pick a comparison that matches the data — plain numbers, human sizes, months, or version strings.

When to use Command
Sort by numeric value (10 after 2) sort -n file.txt
Sort general numeric values (scientific notation) sort -g file.txt
Sort human-readable sizes (1M > 100K) sort -h file.txt
Sort version strings (1.12.2 > 1.1.2) sort -V file.txt
Sort by three-letter month names sort -M file.txt
Shuffle lines randomly sort -R file.txt

Files, output, and merging

Write results to a file, merge already-sorted inputs, or handle NUL-terminated records.

When to use Command
Write sorted output to a file (safe in-place: same path for input and output) sort -o file.txt file.txt
Merge pre-sorted files without resorting sort -m sorted1.txt sorted2.txt
Use NUL instead of newline as the line delimiter sort -z file.list
Read input file list from a NUL-separated file sort --files0-from=list.nul

Checking sorted input

Verify order without rewriting the file — useful in scripts and CI.

When to use Command
Report the first out-of-order line sort -c file.txt
Exit 0 only if already sorted (silent) sort -C file.txt

Help and version

When to use Command
Show built-in usage sort --help
Show implementation version sort --version

sort — command syntax

Synopsis from sort --help on Ubuntu 25.04 (uutils coreutils 0.2.2):

text
sort [OPTION]... [FILE]...

Key format: FIELD[.CHAR][OPTIONS][,FIELD[.CHAR]][OPTIONS] — fields start at 1. Per-key option letters (n, r, h, V, …) override global flags for that key only. sort does not modify system files unless you pass -o to overwrite an output path.


sort — command examples

Essential Sort lines alphabetically

The default mode orders lines by locale rules — the starting point for any text file or command output you want in reading order.

Run the command:

bash
printf '%s\n' zebra Apple banana cherry Banana | sort

Sample output:

text
Apple
Banana
banana
cherry
zebra

Uppercase letters sort before lowercase in the default C/POSIX locale. Add -f when case should not matter.

Essential Sort numbers numerically

Without -n, sort compares digit strings character by character (10 before 2). Use -n for integer-like columns.

Run the command:

bash
printf '%s\n' 5108 152 126 16 529 70 1 990 56 | sort -n

Sample output:

text
1
16
56
70
126
152
529
990
5108

Pipe ls, du, or awk output through sort -n when the column holds counts or byte sizes.

Common Reverse order and ignore case

Combine -r for descending order and -f to treat Apple and apple as the same letter.

Run the command:

bash
printf '%s\n' zebra Apple banana cherry Banana | sort -rf

Sample output:

text
zebra
cherry
banana
Banana
Apple

Use this on mixed-case hostnames or usernames when you want a single alphabetical list.

Common Sort a CSV file by the second column

-t sets the field separator; -k2,2n sorts only field two as a number.

Run the command:

bash
printf '%s\n' 'bob,30' 'alice,25' 'carol,30' | sort -t',' -k2,2n

Sample output:

text
alice,25
bob,30
carol,30

When two rows share the same key (carol and bob both have 30), their relative order depends on the rest of the line unless you add -s.

Common Sort human-readable sizes from du or ls

-h understands suffixes like K, M, and G so 2M sorts above 100K.

Pipe du output through sort -h for largest folders first; see the df and du.

Run the command:

bash
printf '%s\n' 100K 2M 50k 1G | sort -h

Sample output:

text
50k
100K
2M
1G

Pair with du -h or custom awk output when building disk-usage reports.

Common Sort and drop duplicate lines

-u keeps one line per unique value after sorting. For stricter dedup of unsorted input, pipe to uniq instead.

Run the command:

bash
printf '%s\n' dup alpha dup beta dup | sort -u

Sample output:

text
alpha
beta
dup

Equivalent pipeline: sort file.txt | uniq > output.txt.

Common Sort a file in place with -o

-o writes to a named file. Using the same path for input and output replaces the file with sorted lines.

Run the command:

bash
printf '%s\n' line3 line1 line2 > unsorted.txt
sort -o unsorted.txt unsorted.txt
cat unsorted.txt

Sample output:

text
line1
line2
line3

Avoid sort file.txt > file.txt — the shell truncates the file before sort reads it.

Advanced Sort version strings with -V

Version sort treats dotted release numbers sensibly — 1.12.2 sorts after 1.1.2, unlike plain lexical order.

Run the command:

bash
printf '%s\n' 1.12.2 1.1.2 1.2.0 | sort -V

Sample output:

text
1.1.2
1.2.0
1.12.2

Use on package lists, kernel build strings, or semver tags in shell scripts.

Advanced Verify a file is already sorted

-c prints the first disorder and exits non-zero. -C is silent — handy in scripts that only need the exit code.

Run the command:

bash
printf '%s\n' b a c > bad.txt
sort -c bad.txt 2>&1
echo exit:$?

Sample output:

text
sort: bad.txt:2: disorder: a
exit:1

Run sort -C good.txt in CI when a pipeline step must prove log lines arrived in order.

Advanced Merge two already-sorted files

-m combines ordered inputs in linear time without a full resort — useful after splitting huge sorted logs.

Run the command:

bash
printf '%s\n' a c e > m1.txt
printf '%s\n' b d f > m2.txt
sort -m m1.txt m2.txt

Sample output:

text
a
b
c
d
e
f

Each input file must already be sorted on the same key you care about.


sort — when to use / when not

Use sort whenUse something else when
  • You need lines or columns in a defined order in the shell
  • Pipeline output from awk, cut, find -printf, or ls should be ranked
  • You will deduplicate with sort -u or sort \| uniq
  • Files are too large for a spreadsheet but small enough for disk-based sorting
  • You only need filenames in order → ls has built-in sort flags, or list then sort
  • Real-time filesystem search by name → find or locate
  • Structured JSON or SQL queries → jq, a database, or awk for complex records
  • In-place reorder of binary data → not a text sort problem

sort vs ls sorting

ls can sort its own listing (ls -lS by size, ls -lt by time), but sort is the general text tool for any columnar or line-oriented data — file listings, logs, CSV, /etc/passwd fields, or find output. Piping ls -l to sort -k5 -n still works when you need a custom key or stable ordering ls does not offer.


Text and file workflows that usually appear beside sort in scripts.

Command One line
sort Order lines and fields (this page)
uniq Drop adjacent duplicate lines (run after sort)
awk Extract or reshape columns before sorting
cut Pull one field, then pipe to sort

Browse the full index in our Linux commands reference.


sort — interview corner

What does the sort command do in Linux?

sort reads lines from files or standard input and prints them in sorted order. By default it uses locale collation (alphabetical). Flags change the comparison: -n for numbers, -k for key fields, -r for reverse.

Example:

bash
printf '%s\n' c b a | sort

Sample output:

text
a
b
c

A strong answer is:

"sort orders text lines from files or stdin. I use -n for numeric columns, -k for fields, and -o to write results back to a file safely."

How does sort -k work?

-k selects which field (column) drives the comparison. Fields are numbered from 1 and separated by whitespace unless you set -t.

Syntax: sort -k START[,END][OPTIONS]. Example — sort by the second field numerically:

bash
sort -k2,2n data.txt

For ls -l style output, sort -k5 -n sorts by the size column (fifth field).

A strong answer is:

"-k picks the sort key — field number from 1, with optional end field and n for numeric. I combine -t for CSV delimiters and -k2,2n when only one column should drive the order."

Why does sort put 10 before 2 without -n?

Default lexicographic sort compares characters left to right. The first character of 10 is 1, which comes before 2, so 10 appears before 2.

With -n:

bash
printf '%s\n' 10 2 1 | sort -n

Sample output:

text
1
2
10

A strong answer is:

"Without -n, sort compares strings character by character. -n parses fields as numbers so 2 comes before 10."

How do you sort a file in place?

Use -o to name the output file. When input and output are the same path, sort writes to a temp file first:

bash
sort -o file.txt file.txt

Do not use sort file.txt > file.txt — the shell empties the file before sort reads it.

A strong answer is:

"sort -o file.txt file.txt — the -o form is safe for in-place sorts. Redirection to the same path truncates the input."

What is the relationship between sort and uniq?

uniq only removes adjacent duplicate lines. Random duplicates in a file survive uniq until you sort first:

bash
sort file.txt | uniq > unique.txt

Or use sort -u, which sorts and keeps one line per unique value in a single step.

A strong answer is:

"uniq needs duplicates on consecutive lines, so I sort first. sort -u combines both steps when I only need unique sorted lines."


Troubleshooting

Symptom Likely cause Fix
10 before 2 in output Lexicographic default Add -n or -g for numeric data
Empty file after sort f > f Shell truncation Use sort -o f f
CSV columns mis-sorted Wrong delimiter sort -t',' -k2,2n file.csv
sort: disorder with -c File not in expected order Sort with the same keys, or fix upstream
Unknown option on Ubuntu 25.04 uutils vs GNU flag drift Run sort --help on the host; this page matches uutils coreutils 0.2.2
Case order surprises Locale collation LC_ALL=C sort file for byte order, or -f to fold case
Version strings wrong order Lexical sort Use -V for dotted version numbers

References

Omer Cakmak

Linux Administrator

Highly skilled at managing Debian, Ubuntu, CentOS, Oracle Linux, and Red Hat servers. Proficient in bash scripting, Ansible, and AWX central server management, he handles server operations on …