sort

text processingLinux/Unix
The sort command is one of the most frequently used commands in Linux/Unix-like operating systems. sort Sort lines of text files

Quick Reference

Command Name:

sort

Category:

text processing

Platform:

Linux/Unix

Basic Usage:

sort [options] [arguments]

Common Use Cases

    Syntax

    sort [options] [file...]

    Options

    Option Description
    -b, --ignore-leading-blanks Ignore leading blanks
    -c, --check Check if input is already sorted; don't sort
    -d, --dictionary-order Consider only blanks and alphanumeric characters
    -f, --ignore-case Fold lowercase to uppercase characters
    -g, --general-numeric-sort Compare according to general numerical value
    -h, --human-numeric-sort Compare human readable numbers (e.g., 2K, 1G)
    -i, --ignore-nonprinting Consider only printable characters
    -k, --key=POS1[,POS2] Sort via a key; start at POS1 and end at POS2
    -m, --merge Merge already sorted files; don't sort
    -n, --numeric-sort Compare according to string numerical value
    -o, --output=FILE Write result to FILE instead of standard output
    -r, --reverse Reverse the result of comparisons
    -R, --random-sort Shuffle, but group identical keys (same as --random-source=FILE)
    -s, --stable Stabilize sort by disabling last-resort comparison
    -t, --field-separator=SEP Use SEP instead of non-blank to blank transition
    -u, --unique With -c, check for strict ordering; without -c, output only the first of equal lines
    -V, --version-sort Natural sort of (version) numbers within text
    --help Display help and exit
    --version Output version information and exit

    Examples

    How to Use These Examples

    The examples below show common ways to use the sort command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    sort file.txt
    Sort a file in ascending order.
    sort -r file.txt
    Sort a file in descending (reverse) order.
    sort -n file.txt
    Sort a file numerically. # Advanced Examples Advanced sort -k2,2 file.txt Sort a file based on the second field. sort -t"," -k3,3n data.csv Sort a CSV file numerically by the third column. sort -u file.txt Sort and remove duplicate lines.
    cat file.txt | sort | uniq -c
    Count occurrences of each line. sort -R file.txt Shuffle lines randomly. sort -h file.txt Sort human-readable numbers (like 2K, 1G). sort -k1,1 -k2,2n file.txt Sort by first field, then numerically by second field. # Merge two sorted files sort -m sorted1.txt sorted2.txt # Sort IP addresses naturally sort -V ip_addresses.txt # Sort dates (YYYY-MM-DD format) sort -k1.1,1.4n -k1.6,1.7n -k1.9,1.10n dates.txt # Case-insensitive sort sort -f words.txt # Save sorted output to a new file sort input.txt -o output.txt # Sort a file and check if it's already sorted sort -c file.txt # Sort with custom temporary directory (useful for large files) sort --temporary-directory=/tmp/sorting file.txt

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `sort` command is a powerful text processing utility in Unix-like operating systems that arranges lines of text files in a specified order. It's an essential tool for data manipulation, analysis, and organization in the command line environment. At its most basic, `sort` takes one or more text files as input and outputs the lines in ascending alphabetical order. However, its true power lies in its extensive options that allow for highly customized sorting operations. Key features of the `sort` command include: 1. Flexible Sort Orders: Beyond basic alphabetical sorting, `sort` can handle numeric sorting (`-n`), human-readable number sorting (`-h`), month sorting, reverse sorting (`-r`), and even random shuffling (`-R`). 2. Field-Based Sorting: Using the `-k` option, `sort` can sort based on specific fields (columns) within each line. This is especially useful for structured data like CSV files or tabular output. 3. Custom Delimiters: The `-t` option allows specifying custom field separators, making it versatile for various data formats. 4. Duplicate Handling: The `-u` option removes duplicate lines, effectively combining sorting and uniquing operations. 5. Merge Capability: With the `-m` option, `sort` can efficiently merge multiple pre-sorted files without resorting them. 6. Stability: The `-s` option ensures that records with equal keys maintain their original order (stable sort). 7. Case Sensitivity Control: The `-f` option enables case-insensitive sorting. 8. Large File Support: `sort` is designed to handle files larger than available memory through efficient external sorting algorithms. Common use cases for the `sort` command include: - Organizing log files or any text data for easier analysis - Preparing data for further processing with tools like `uniq` or `join` - Creating alphabetical listings of words, names, or other textual data - Sorting numerical data like sizes, dates, or IP addresses in logical order - Removing duplicates from datasets while preserving order - Performing multi-key sorts on complex structured data The `sort` command plays a central role in the Unix philosophy of creating small, specialized tools that can be combined in powerful ways. It's frequently used in pipelines with other text-processing commands like `cut`, `awk`, `uniq`, and `grep` to perform sophisticated data transformations and analyses. While simple in concept, mastering the various options of `sort` allows for handling complex sorting requirements efficiently from the command line without requiring specialized data processing software.

    Related Commands

    These commands are frequently used alongside sort or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the sort command works in different scenarios.

    $ sort
    View All Commands