Linux – sort Command

The sort command in Linux is used to sort lines of text files. It can arrange the text in various orders, such as alphabetical, numerical, or based on specific fields. This is useful for organizing and managing data within text files.



Tutorials dojo strip

Initial Example

Using sort to sort the lines of a file called example.txt in alphabetical order:

sort example.txt




SORT Parameters

ParameterDescription
-b, --ignore-leading-blanksIgnore leading blank spaces in sorting.
-d, --dictionary-orderOnly consider blanks and alphanumeric characters for sorting.
-f, --ignore-caseTreat lowercase and uppercase characters as equivalent.
-g, --general-numeric-sortSort lines based on general numerical value.
-i, --ignore-nonprintingIgnore non-printable characters in sorting.
-M, --month-sortSort by month, with unknowns < ‘JAN’ < … < ‘DEC’.
-h, --human-numeric-sortSort human-readable numbers (e.g., 2K, 1G).
-n, --numeric-sortSort lines based on numerical value.
-R, --random-sortShuffle lines randomly, keeping identical keys grouped.
--random-source=FILEUse FILE for random byte generation.
-r, --reverseReverse the sort order.
--sort=WORDSort based on WORD: general-numeric, human-numeric, month, numeric, random, version.
-V, --version-sortSort version numbers within text naturally.
--batch-size=NMERGEMerge up to NMERGE inputs simultaneously; use temporary files for more.
-c, --check, --check=diagnose-firstCheck if input is sorted, without sorting.
-C, --check=quiet, --check=silentLike -c, but suppress the output of the first unsorted line.
--compress-program=PROGCompress and decompress temporary files using PROG.
--debugAnnotate sorted line parts and warn about potential issues.
--files0-from=FRead input from files specified by NUL-terminated names in F; use - for standard input.
-k, --key=KEYDEFSort based on a specific key; KEYDEF defines the key’s location and type.
-m, --mergeMerge already sorted input files without sorting them.
-o, --output=FILEWrite output to FILE instead of standard output.
-s, --stableEnsure stable sorting by disabling the last-resort comparison.
-S, --buffer-size=SIZEUse SIZE for the main memory buffer.
-t, --field-separator=SEPUse SEP as the field separator instead of whitespace.
-T, --temporary-directory=DIRUse DIR for temporary files; specify multiple directories with multiple options.
--parallel=NRun N sorts concurrently.
-u, --uniqueWith -c, check for strict order; without -c, output only the first of equal lines.
-z, --zero-terminatedUse NUL as the line delimiter instead of newline.
--helpDisplay help information and exit.
--versionShow version information and exit.




Examples

1. Sort Lines Alphabetically

To sort the lines of a file in alphabetical order:

sort filename.txt

2. Sort Lines Numerically

To sort the lines of a file based on numerical values:

sort -n filename.txt

3. Sort Lines in Reverse Order

To sort the lines of a file in reverse order:

sort -r filename.txt

4. Sort Based on a Specific Field

To sort the lines of a file based on the values in a specific field (e.g., the second field):

sort -k 2 filename.txt

5. Sort with a Custom Field Separator

To specify a custom field separator (e.g., a comma):

sort -t, -k 2 filename.txt

6. Remove Duplicate Lines

To sort and remove duplicate lines from a file:

sort -u filename.txt

7. Sort Multiple Files

To sort the contents of multiple files and combine the output:

sort file1.txt file2.txt -o sorted_output.txt




Linux Playground

Scroll to Top