LinuxCommandLibrary

sort

Sort lines of text files

TLDR

Sort a file in ascending order

$ sort [path/to/file]
copy

Sort a file in descending order
$ sort [[-r|--reverse]] [path/to/file]
copy

Sort a file in case-insensitive way
$ sort [[-f|--ignore-case]] [path/to/file]
copy

Sort a file using numeric rather than alphabetic order
$ sort [[-n|--numeric-sort]] [path/to/file]
copy

Sort /etc/passwd by the 3rd field of each line numerically, using ":" as a field separator
$ sort [[-t|--field-separator]] [:] [[-k|--key]] [3n] [/etc/passwd]
copy

As above, but when items in the 3rd field are equal, sort by the 4th field by numbers with exponents
$ sort [[-t|--field-separator]] [:] [[-k|--key]] [3,3n] [[-k|--key]] [4,4g] [/etc/passwd]
copy

Sort a file preserving only unique lines
$ sort [[-u|--unique]] [path/to/file]
copy

Sort a file, printing the output to the specified output file (can be used to sort a file in-place)
$ sort [[-o|--output]] [path/to/file] [path/to/file]
copy

SYNOPSIS

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

PARAMETERS

-b, --ignore-leading-blanks
    Ignore leading blanks.

-d, --dictionary-order
    Consider only blanks and alphanumeric characters.

-f, --ignore-case
    Fold lower case to upper case characters.

-g, --general-numeric-sort
    Compare according to general numerical value.

-i, --ignore-nonprinting
    Ignore nonprinting characters.

-M, --month-sort
    Compare (unknown) MONTH abbreviations.

-h, --human-numeric-sort
    Compare human readable numbers (e.g., 2K, 1G).

-n, --numeric-sort
    Compare according to string numerical value.

-r, --reverse
    Reverse the result of comparisons.

-V, --version-sort
    Sort version strings naturally.

-o, --output=FILE
    Write result to FILE instead of standard output.

-u, --unique
    With -c, check for strict ordering; without -c, output only the first of an equal run.

-k, --key=POS1[,POS2]
    Start a key at POS1, end it at POS2 (origin 1).

-t, --field-separator=SEP
    Use SEP instead of non-blank to blank transition.

DESCRIPTION

The sort command in Linux is a powerful utility used to arrange lines of text files in a specific order. By default, sort treats each line as a string and orders them lexicographically. However, it offers a rich set of options to customize the sorting behavior. It can sort numerically, reverse the order, ignore case, remove duplicate lines, and even sort based on specific fields within each line. The sort command reads input from files specified as arguments or from standard input. The sorted output can be displayed on the terminal or redirected to a new file.
Using the sort command efficiently can dramatically simplify data analysis and manipulation tasks.

CAVEATS

The sort command might be slow for very large files. Consider using external sorting tools for extremely large datasets. Locale settings can influence sorting behavior, especially when dealing with non-ASCII characters.

RETURN STATUS

Exit status is 0 if no error occurs. Exit status is 1 if an error occurs.

PERFORMANCE CONSIDERATIONS

For optimal performance with large datasets, ensure sufficient memory is available. Consider using the -T option to specify a temporary directory for intermediate files, especially when sorting exceeds available memory.

HISTORY

The sort command has been a staple of Unix-like operating systems since their early days. It originated in the early Unix versions, providing a basic but essential text processing capability. Over time, it has been enhanced with various options to handle different sorting scenarios, numeric values, and field-based sorting. Its consistent presence and evolution reflect its enduring value in text manipulation tasks.

SEE ALSO

uniq(1), tac(1), rev(1)

Copied to clipboard