Text
sort
Sort lines of text.
textsortlinesnumericunique
Additional Notes
sort orders lines of text. It is often used with pipelines to organize command output, logs, lists, and reports.
Sorting can be alphabetical, numeric, reverse, human-readable size aware, or based on a field.
Syntax
sort [options] [file...]
Parameters
options: Flags that change howsortbehaves.file: Text file to read or process.
Common Options
-n,--numeric-sort: Sort numbers by numeric value.-h,--human-numeric-sort: Sort human-readable sizes like2K,10M,1G.-r,--reverse: Reverse order.-u,--unique: Remove duplicate lines after sorting.-k N: Sort by field/key.-t SEP: Use a field separator.-o FILE: Write output to a file.
Examples
sort names.txt
Sort lines alphabetically.
sort -n numbers.txt
Sort numbers correctly.
sort -r names.txt
Sort in reverse order.
du -sh * | sort -h
Sort disk usage by human-readable size.
sort -u words.txt
Sort and remove duplicates.
sort -t: -k3 -n /etc/passwd
Sort /etc/passwd by numeric UID field.
Practical Notes
- Use
sort -nfor plain numbers; normal sort compares text. - Use
sort -hfor sizes from commands likedu -h. uniqusually needs sorted input to remove all duplicates.- Use
LC_ALL=C sortfor faster byte-based sorting in some scripts.