Sort Command - File Sorting Utility | Online Free DevTools by Hexmos

Learn how to sort files with the sort command. Discover options for unique sorting, reverse order, random sorting, output redirection, and temporary directory usage.

Sort Command Examples

The sort command is a powerful utility in Unix-like operating systems used to sort lines of text files. It can sort alphabetically, numerically, and with various options to customize the output. Below are common usage examples:

Sort File Content

To sort a file alphabetically, use the following syntax:

sort <file>

Sort and Keep Unique Lines

To sort a file and remove duplicate lines, keeping only unique entries:

sort -u <file>

Sort in Reverse Order

To sort a file in reverse (descending) order:

sort -r <file>

Sort File Randomly

To sort the lines of a file in a random order:

sort -R <file>

Sort and Save to Another File

To sort a file and store the output in a different file:

sort <inputFile> -o <outputFile>

Handling Large Files with Temporary Directories

By default, sort uses /var/tmp for temporary files. If this directory has limited space and you need to sort very large files, you can specify an alternative directory with adequate space using the -T option:

sort -T <tempDirectory> <file>

For more advanced sorting options, including sorting by specific fields and different character sets, refer to the official sort man page.