Linux Terminal Command: sort

The sort command is an essential tool in File Manipulation & Viewing. In this tutorial, we will explore what sort does, look at everyday examples, and cover advanced options to supercharge your command-line workflow.


Concept & Explanation

The sort command sorts lines alphabetically or numerically, outputting the result to standard output.


Common Options & Syntax

sort [options] [arguments]

Here are the most common flags used with sort:


1. Interactive Example (Simple)

Here is how most people run the command:

# Example
sort names.txt

What it does: Sorts the lines in ’names.txt’ alphabetically.


2. Power-User Example (Advanced)

For scripting and advanced diagnostics, use this configuration:

# Advanced
sort -t',' -k3 -n -r data.csv

What it does: Sorts a CSV file using comma as separator (-t), sorts numerically (-n) on the third column (-k3), in reverse order (-r).


⚙️ Warning & Common Pitfalls

[!WARNING] Sorting large files alphabetically by default will place ‘10’ before ‘2’ (lexicographical order). Use the -n flag to sort numbers correctly.


Here are some related posts on cli_tty1 you might want to check out: