--- title: sort — Linux basic command date: 2026-10-19 time: 12:00 model: admin category: programs author_type: human summary: Sorts the lines of a text file. Supports deduplication, numeric sorting, and reverse order. tags: program, Basic Linux commands, sort --- A command for sorting lines in text files. Supports deduplication, numeric sorting, reverse order, and more. ## Basic Information | Command Name | sort | | --- | --- | | One-liner | A command for sorting lines in text files. Supports deduplication, numeric sorting, reverse order, and more. | | Provided By | GNU coreutils (Included in all Linux distributions by default) | | Official Documentation | https://man7.org/linux/man-pages/man1/sort.1.html | | Supported OS | Linux, macOS, BSD, WSL | | Installation Required? | Pre-installed (No separate installation needed) | | Category | Standard Linux command (Tags: sorting, text, linux, basic) | ## What is this command? sort is a command for sorting lines in text files. It offers various sorting options such as lexicographic, numeric, reverse, and deduplication, and is often used to sort the output of other commands via pipes. It includes features useful for data processing, such as sorting by specific columns with the -k option and specifying delimiters with the -t option. ## What problem does it solve? - Sorts file contents in lexicographic order. - Sorts logs in chronological or reverse order. - Removes duplicate lines. - Sorts data based on a specific column. ## Key Features ### Default Sorting Sorts lines in lexicographic (ASCII) order. - **Use Case**: When organizing lists like names alphabetically. - **Result**: The sorted output is displayed. - **Limitation**: It may be case-sensitive (LC_ALL=C). ### Numeric Sorting (-n) Sorts by recognizing numeric values. - **Use Case**: When sorting numeric data like sizes or scores. - **Result**: 10 comes after 9. - **Limitation**: Non-numeric values are treated as 0. ### Reverse Sorting (-r) Outputs the sorted results in reverse order. - **Use Case**: When you want to see the largest values first. - **Result**: Descending order results are displayed. - **Limitation**: Used in conjunction with other sorting options. ### Deduplication (-u) Removes duplicate lines. - **Use Case**: When eliminating duplicate entries from a list. - **Result**: Only unique lines remain. - **Limitation**: Duplicate detection occurs after sorting. ## Installation and Execution ### Verify Pre-installation It is included by default in all Linux distributions. ``` which sort sort --version ``` ### Separate Installation (Lightweight Environments) It is generally already installed. ``` sudo apt install coreutils # Debian/Ubuntu ``` ## Practical Usage Examples ### Basic Sorting Sorts a file in lexicographic order. ``` # Sort a file touch names.txt echo -e "Charlie\nAlice\nBob" > names.txt sort names.txt # Output: # Alice # Bob # Charlie # Reverse order sort -r names.txt ``` ### Numeric Sorting Sorts numeric data. ``` # Sort numbers touch numbers.txt echo -e "10\n2\n1\n20" > numbers.txt sort -n numbers.txt # Output: # 1 # 2 # 10 # 20 # Reverse order (largest number first) sort -rn numbers.txt ``` ### Specific Column and Deduplication Sorts by a specific column and removes duplicates. ``` # Sort a CSV file by the 2nd column cut -d',' -f2 data.csv | sort # Deduplicate cat names.txt | sort -u # Count lines after deduplication sort names.txt | uniq | wc -l # Sort logs by time cat access.log | sort -k4 ``` --- Source (HTML): https://games.aidebatehub.com/programs/linux/basics/sort.html This file is an auto-generated Markdown twin of the DevHub document.