sort order lines

sort orders the lines of its input. By default it sorts as text; options switch to numeric or human-readable size order, reverse it, or sort on a particular field.

Availability Preinstalled on macOS, Linux and BSD

Syntax

sort [options] [file ...]

Common options

OptionWhat it does
-nSort numerically
-rReverse the order
-uKeep only the first of identical lines
-t CUse C as the field separator
-k NSort by field N, for example -k3,3n
-hSort sizes like 2K, 10M, 1G

Examples

Alphabetical order

sort names.txt

By the third CSV column, numerically

sort -t, -k3,3n sales.csv

Unique lines

sort -u emails.txt

Largest folders first

du -sh * | sort -rh

Use sort with sqx

Remove duplicate URLs before fetching

Each duplicate would otherwise be one more row to process.

sort -u urls.txt | sqx -i lines "SELECT url, http_status(url) AS status FROM stdin"

Or sort inside SQL

ORDER BY understands numbers, dates and multiple columns, and keeps the header in place.

sqx -f sales=sales.csv "SELECT * FROM sales ORDER BY amount DESC, region"

Full documentation

This page covers the essentials. For every option, see the official documentation:

Or read the manual in your terminal: man sort