info > sort(1)

๐Ÿ“‹ 7.1 'sort': Sort text files

๐Ÿš€ Quick Reference

Use Case Command Description
๐Ÿ“ Sort alphabetically sort Sort lines in ascending order (default)
๐Ÿ”ข Sort numerically sort -n Sort by numeric value of the line
๐Ÿ”ฝ Reverse order sort -r Sort in descending order
๐Ÿ”‘ Sort by a specific field sort -k 2,2 Sort using the second field only
โœ… Check if file is sorted sort -c Exit 0 if sorted, 1 otherwise with diagnostic
๐Ÿ”— Merge sorted files sort -m Merge already-sorted files (faster)
๐Ÿ“Š Humanโ€‘readable sizes sort -h Sort by numeric value with SI suffixes (K, M, G, โ€ฆ)
๐ŸŽฒ Random sort sort -R Permute input lines randomly
๐Ÿ” Unique sort sort -u Output only the first of equal lines
๐Ÿท๏ธ Version sort sort -V Sort by version number (e.g., 1.10 > 1.9)
๐Ÿ—‚๏ธ Field separator sort -t ':' Use colon as field delimiter
๐Ÿ”ฌ Debug mode sort --debug Highlight the sort key and warn about usage

๐Ÿ“– Synopsis

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

๐Ÿ“ Description

'sort' sorts, merges, or compares all the lines from the given files, or standard input if none are given or for a FILE of '-'. By default, 'sort' writes the results to standard output.

Many options affect how 'sort' compares lines; if the results are unexpected, try the '--debug' option to see what happened. A pair of lines is compared as follows: 'sort' compares each pair of fields (see '--key'), in the order specified on the command line, according to the associated ordering options, until a difference is found or no fields are left. If no key fields are specified, 'sort' uses a default key of the entire line. Finally, as a last resort when all keys compare equal, 'sort' compares entire lines as if no ordering options other than '--reverse' ('-r') were specified. The '--stable' ('-s') option disables this "last-resort comparison" so that lines in which all fields compare equal are left in their original relative order. The '--unique' ('-u') option also disables the last-resort comparison.

Unless otherwise specified, all comparisons use the character collating sequence specified by the 'LC_COLLATE' locale. A line's trailing newline is not part of the line for comparison purposes. If the final byte of an input file is not a newline, GNU 'sort' silently supplies one. GNU 'sort' (as specified for all GNU utilities) has no limit on input line length or restrictions on bytes allowed within lines.

โš™๏ธ Operation Modes

'sort' has three modes of operation: sort (the default), merge, and check for sortedness. The following options change the operation mode:

๐Ÿšช Exit Codes

๐ŸŒ Environment

If the environment variable TMPDIR is set, 'sort' uses its value as the directory for temporary files instead of '/tmp'. The '--temporary-directory' ('-T') option in turn overrides the environment variable.

๐Ÿ”ง Options

๐Ÿ“Š Sorting Order Options

These options affect the ordering of output lines. They may be specified globally or as part of a specific key field. If no key fields are specified, global options apply to comparison of entire lines; otherwise the global options are inherited by key fields that do not specify any special options of their own. In pre-POSIX versions of 'sort', global options affect only later key fields, so portable shell scripts should specify global options first.

๐Ÿ› ๏ธ Other Options

๐Ÿ“š Examples

Here are some examples to illustrate various combinations of options.

๐Ÿ“œ Historical Notes

Historical (BSD and System V) implementations of 'sort' have differed in their interpretation of some options, particularly '-b', '-f', and '-n'. GNU sort follows the POSIX behavior, which is usually (but not always!) like the System V behavior. According to POSIX, '-n' no longer implies '-b'. For consistency, '-M' has been changed in the same way. This may affect the meaning of character positions in field specifications in obscure cases. The only fix is to add an explicit '-b'.

A position in a sort field specified with '-k' may have any of the option letters 'MbdfghinRrV' appended to it, in which case no global ordering options are inherited by that particular field. The '-b' option may be independently attached to either or both of the start and end positions of a field specification, and if it is inherited from the global options it will be attached to both. If input lines can contain leading or adjacent blanks and '-t' is not used, then '-k' is typically combined with '-b' or an option that implicitly ignores leading blanks ('Mghn') as otherwise the varying numbers of leading blanks in fields can cause confusing results.

If the start position in a sort field specifier falls after the end of the line or after the end field, the field is empty. If the '-b' option was specified, the '.C' part of a field specification is counted from the first nonblank character of the field.

On systems not conforming to POSIX 1003.1-2001, 'sort' supports a traditional origin-zero syntax '+POS1 [-POS2]' for specifying sort keys. The traditional command 'sort +A.X -B.Y' is equivalent to 'sort -k A+1.X+1,B' if Y is '0' or absent, otherwise it is equivalent to 'sort -k A+1.X+1,B+1.Y'.

This traditional behavior can be controlled with the '_POSIX2_VERSION' environment variable; it can also be enabled when 'POSIXLY_CORRECT' is not set by using the traditional syntax with '-POS2' present.

Scripts intended for use on standard hosts should avoid traditional syntax and should use '-k' instead. For example, avoid 'sort +2', since it might be interpreted as either 'sort ./+2' or 'sort -k 3'. If your script must also run on hosts that support only the traditional syntax, it can use a test like if sort -k 1 </dev/null >/dev/null 2>&1; then ... to decide which syntax to use.

๐Ÿ“ Footnotes

If you use a non-POSIX locale (e.g., by setting 'LC_ALL' to 'en_US'), then 'sort' may produce output that is sorted differently than you're accustomed to. In that case, set the 'LC_ALL' environment variable to 'C'. Note that setting only 'LC_COLLATE' has two problems. First, it is ineffective if 'LC_ALL' is also set. Second, it has undefined behavior if 'LC_CTYPE' (or 'LANG', if 'LC_CTYPE' is unset) is set to an incompatible value. For example, you get undefined behavior if 'LC_CTYPE' is 'ja_JP.PCK' but 'LC_COLLATE' is 'en_US.UTF-8'.

sort(1)
๐Ÿ“‹ 7.1 'sort': Sort text files ๐Ÿš€ Quick Reference ๐Ÿ“– Synopsis ๐Ÿ“ Description โš™๏ธ Operation Modes ๐Ÿšช Exit Codes ๐ŸŒ Environment ๐Ÿ”ง Options
๐Ÿ“Š Sorting Order Options ๐Ÿ› ๏ธ Other Options
๐Ÿ“š Examples ๐Ÿ“œ Historical Notes ๐Ÿ“ Footnotes

Generated by phpman v4.9.26-1-g511901d · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-08-02 06:22 @216.73.217.9
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Valid XHTML 1.0 Transitional!Valid CSS!
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format

^_top_^