How to Use the sort Command in Linux

How to Use the sort Command in Linux

A list of numbers in a file may look out of order when Linux compares each line as text. The sort command can compare numeric values instead and write the lines in numeric order.

I’ll show how to sort numbers in either direction and save or check the result.

TL;DR

Use sort to order text lines, adding an option when values should compare as numbers or by a field.

  • sort file.txt orders complete lines using the current locale.
  • sort -n file.txt compares numeric values, and sort -r reverses the comparison.
  • Use -k to select a key and -t to set a delimiter.

What Does the Linux sort Command Do?

sort reads a file, compares its lines, and writes the ordered result to standard output. The input remains unchanged unless you choose an output destination.

Without a key option, GNU sort compares whole lines using the active locale’s collation rules.

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

When FILE is omitted or set to a hyphen, sort reads standard input, so another command can send lines through a pipe. A pipeline orders command results without an intermediate file.

For example, find can supply file names and sort can place those names in order. I chose small named text files for the examples below so each comparison uses known input, and the visible terminal capture shows the whole-line command.

How to Use sort to Order Lines and Fields

Choose a comparison for whole-line text, numbers, or a delimited field. The examples use a small file so each option’s effect stays visible.

Step 1: Sort complete lines

Run sort followed by a filename to order each line as text. The command I ran was sort sort-demo.txt.

Apple
banana
pear
Terminal output from sorting three lines with sort

The output is sorted, and the input file remains unchanged. For command output instead of a file, use producer | sort.

Step 2: Compare numbers and reverse the order

Add -n when each line begins with a number, then add -r when you want descending order. The command sort -n sort-numbers.txt returned the values in numeric order.

4
18
32
105

For descending numeric order, combine the options as sort -nr sort-numbers.txt. A leading minus sign and decimal values are handled by numeric comparison, while units such as KB call for -h.

Step 3: Sort by a delimited field

On this comma-separated file, I ran sort -t, -k1,1n sort-fields.csv. Its bounded key compares only the first field numerically.

7,Juice
28,Coffee
104,Tea

Without the comma delimiter, the default fields are separated by transitions between blanks and non-blanks. A key such as -k2 can continue through later fields for tie-breaking, while -k2,2 limits the comparison to field two.

Step 4: Save or validate sorted output

Use -o to save a sorted result, including when the destination is the input file. This lets sort finish reading the input before replacing it.

sort -o sorted.txt input.txt
sort -c sorted.txt

Run sort -c sorted.txt in a shell script, then branch on its exit status because an unsorted file returns status 1.

Common Errors with sort

Unexpected ordering usually comes from the comparison rule, locale, or field boundary. Check these cases before changing the input data.

CaseWhat happensAdjustment
Text order differs across systemsLC_COLLATE sets character collationSet a deliberate locale for repeatable scripts
Numbers appear out of orderDefault comparison is textualUse -n, or -h for human-readable sizes
CSV fields contain commas-t, splits fields at every comma, including quoted dataUse a CSV-aware parser for quoted CSV records
Equal selected keys move unpredictablyFurther fields can break tiesUse -s to preserve the input order of equal keys
Repeated keys should appear onceOrdinary sort retains matching linesUse -u for one line per equal key
Version strings order lexicallyText order places release-10 before release-2Use -V for version order

Quoted CSV fields need a CSV-aware parser because sort splits at the selected separator without interpreting quotes. This distinction matters when commas can occur inside a field.

For related command-line tasks, see our guides to the man command, find command, and tr command. Use diff to compare files before or after sorting, or review our Linux command-line guide for pipeline basics. GNU’s sort documentation details less common options.

Conclusion: Choose the Comparison That Fits

Start with the value that should determine the order, then use that rule consistently. This keeps the result meaningful when you reuse it in a pipeline or a later check.

FAQ

These answers cover common choices when sorting lines on Linux.

How do I sort numbers in Linux?

Use sort -n filename to compare numeric values, and add -r for descending order.

How do I sort by a column?

Use -k to choose the key and -t to define a delimiter when fields are separated by a character such as a comma.

Does sort change the original file?

No. By default, sort writes results to standard output. Use -o to write the sorted result to a file.