Linux command
sort 命令
文件
复制后可按需替换文件名、目录或参数。
常用示例
Sort a file alphabetically
sort [file]
Sort in reverse order
sort -r [file]
Sort numerically
sort -n [file]
Sort by specific column
sort -k2 [file]
Sort by multiple columns
sort -k1,1 -k2,2n [file]
Sort unique lines only
sort -u [file]
Sort case-insensitively
sort -f [file]
Sort and save to file
sort [file] -o [output_file]
Sort human-readable sizes
sort -h [file]
说明
sort sorts lines of text files according to specified criteria. By default, it performs lexicographic (dictionary) sorting using the current locale. Multiple input files are merged and sorted together. Output goes to stdout by default; use -o to write to a file (can be same as input file safely). The -k option is powerful for structured data. Combined with -t to set the delimiter, it can sort CSV, TSV, and other columnar data by specific fields. For large files, sort automatically uses temporary files and can utilize multiple CPU cores with --parallel.
参数
- -r, --reverse
- Reverse the sort order
- -n, --numeric-sort
- Compare according to string numerical value
- -h, --human-numeric-sort
- Compare human-readable numbers (2K, 1G)
- -f, --ignore-case
- Fold lowercase to uppercase (case-insensitive)
- -u, --unique
- Output only unique lines
- -k _KEYDEF_
- Sort by specified key/column
- -t _SEP_
- Use SEP as field separator
- -o _FILE_
- Write result to FILE instead of stdout
- -c, --check
- Check if input is sorted; exit with status
- -s, --stable
- Stabilize sort by disabling last-resort comparison
- -m, --merge
- Merge already sorted files
- -b, --ignore-leading-blanks
- Ignore leading blanks in keys
- -d, --dictionary-order
- Consider only blanks and alphanumeric characters
- -g, --general-numeric-sort
- Compare according to general numerical value
- -i, --ignore-nonprinting
- Consider only printable characters
- -M, --month-sort
- Compare (unknown) < 'JAN' < ... < 'DEC'
- -R, --random-sort
- Shuffle, but group identical keys
- -V, --version-sort
- Natural sort of version numbers
- -z, --zero-terminated
- Line delimiter is NUL, not newline
- -S, --buffer-size=_SIZE_
- Use SIZE for main memory buffer
- -T, --temporary-directory=_DIR_
- Use DIR for temporaries instead of $TMPDIR or /tmp
- --parallel=_N_
- Use N parallel threads
- --debug
- Annotate the sort key used and warn about questionable usage
FAQ
What is the sort command used for?
sort sorts lines of text files according to specified criteria. By default, it performs lexicographic (dictionary) sorting using the current locale. Multiple input files are merged and sorted together. Output goes to stdout by default; use -o to write to a file (can be same as input file safely). The -k option is powerful for structured data. Combined with -t to set the delimiter, it can sort CSV, TSV, and other columnar data by specific fields. For large files, sort automatically uses temporary files and can utilize multiple CPU cores with --parallel.
How do I run a basic sort example?
Run `sort [file]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -r, --reverse do in sort?
Reverse the sort order