Skip to main content

sort Cheatsheet

By Dejan Panovski Updated on Download PDF

Quick reference for sorting text lines with sort in Linux

The `sort` command orders lines from files or standard input. This cheatsheet covers common sort options for numeric sorting, field-based sorting, unique lines, and practical pipeline usage.

Basic Syntax

Core sort command forms.

CommandDescription
sort file.txtSort lines alphabetically (ascending)
sort -r file.txtSort lines in reverse order
sort -n file.txtSort numerically
sort -nr file.txtSort numerically, largest first
sort file1 file2Merge and sort multiple files

Sort by Field

Sort lines by one column or key.

CommandDescription
sort -k2 file.txtSort by second field
sort -k2,2 file.txtSort using only field 2 as key
sort -t: -k3,3n /etc/passwdSort by UID field in /etc/passwd
sort -t, -k1,1 data.csvSort CSV by first column
sort -k3,3 -k1,1 file.txtSecondary sort: key 3, then key 1

Numeric and Human Sizes

Handle numbers and size suffixes correctly.

CommandDescription
sort -n numbers.txtNumeric sort
sort -g values.txtGeneral numeric sort (floats/scientific notation)
sort -h sizes.txtSort human-readable sizes (K, M, G)
du -sh /var/* | sort -hSort du output by size
sort -V versions.txtNatural version sort (1.9 before 1.10)

Unique and Check

Detect duplicates and verify sorted input.

CommandDescription
sort -u file.txtSort and remove duplicate lines
sort file.txt | uniqEquivalent two-step unique output
sort file.txt | uniq -cCount duplicate occurrences
sort -c file.txtCheck if file is sorted
sort -C file.txtQuiet sorted-check (status code only)

In-Place and Output

Safe ways to write sorted results.

CommandDescription
sort file.txt > sorted.txtWrite output to a new file
sort -o file.txt file.txtSort in place safely
sort -o out.txt in.txtWrite sorted output with -o
sort -T /tmp bigfile.txtUse temp directory for large sorts
LC_ALL=C sort file.txtByte-order sort for predictable locale behavior

Common Pipelines

Practical combinations with other commands.

CommandDescription
grep -Eo '[[:alnum:]_]+' file.txt | sort | uniq -c | sort -rnTop repeated words
cut -d',' -f2 data.csv | sortSort one CSV column
sort access.log | uniq -c | sort -rn | headMost common log lines
ps -eo user= | sort | uniq -c | sort -rnProcess count by user
find . -type f | sortSorted file listing

Troubleshooting

Quick checks for common sort issues.

IssueCheck
Numbers sorted as textAdd -n (or -g for floats)
Size values ordered incorrectlyUse -h for human-readable sizes
Unexpected uppercase/lowercase orderUse -f or set locale with LC_ALL=C
File was emptied after sortingDo not use sort file > file; use sort -o file file
Different results across systemsVerify locale and use explicit key options (-k)

Use these guides for full sorting and text-processing workflows.

GuideDescription
sort Command in LinuxFull sort guide with examples
uniqRemove and count duplicate lines after sorting
head Command in LinuxShow the first lines of output
wc Command in LinuxCount lines, words, and bytes
cut Command in LinuxExtract fields from text
grep Command in LinuxSearch and filter matching lines