File Viewing & Editing

sort

Sort lines of text files alphabetically, numerically, or by custom keys.

Synopsis

syntax
sort [OPTION]... [FILE]...

Examples

Sort lines alphabetically
sort names.txt
Sort numerically in descending order
sort -n -r scores.txt
Sort CSV by second column
sort -t',' -k2 data.csv
Sort directory sizes human-readable
du -sh * | sort -h

Common options

FlagDescription
-nNumeric sort
-rReverse order
-kSort by specified key/field
-uOutput only unique lines
-tField separator character
-hHuman-numeric sort (2K, 1G)

About sort

The `sort` command sort lines of text files alphabetically, numerically, or by custom keys. Text viewing and editing commands are fundamental tools in any Linux user's toolkit.

Linux treats almost everything as a file, so the ability to quickly inspect, filter, transform, and edit file contents from the command line is critical. These commands are regularly combined with pipes and redirects to build powerful data-processing pipelines.

The command accepts 6 commonly used flags shown above, though the full set of options is available in the man page (`man sort`). The 4 examples on this page cover typical real-world usage patterns that you can copy and adapt for your own workflows.

Related commands

More File Viewing & Editing Commands

Other commands in the File Viewing & Editing category

Related tools