VMware ESXi and vSphere Cluster Management

Sort Lines in a Text File with the Linux sort Command

Learn how to sort text-file lines alphabetically, reverse the order, and check whether a file is already sorted with Linux sort.

What the sort command does

sort is a Linux command that reads text input line by line and emits those lines in a selected order. A line is one newline-delimited record in a text file.

A text file contains readable characters arranged into lines. Sorting can organize names, word lists, inventories, hostnames, or other records that were entered in an unordered sequence.

On Raspberry Pi systems, including systems historically called Raspbian, sort is the standard Linux utility. The same basic commands work in other Linux terminal environments.

Example input file

Create a file named fruit.txt containing three deliberately unordered lines:

pear
apple
banana

For example, you can create or edit this file with a text editor, then display its original contents with:

cat fruit.txt

The original contents are:

pear
apple
banana

Sort lines alphabetically

The basic syntax is:

sort FILE

Run it with the example file:

sort fruit.txt

The result is printed in the terminal:

apple
banana
pear

By default, sort uses lexicographic ordering. This is text-based ordering, often described as alphabetical order for simple lowercase words. It compares characters rather than automatically understanding quantities, dates, or the meaning of a record.

The command writes the result to standard output, the terminal output stream used by commands by default. It does not modify fruit.txt. Running cat fruit.txt afterward still shows the original order.

To save the sorted output in a different file, use shell redirection:

sort fruit.txt > sorted-fruit.txt

This leaves fruit.txt unchanged and writes the sorted lines to sorted-fruit.txt. Always understand the output destination before using a workflow that replaces an existing file.

Sort in reverse order

Use the -r option for reverse alphabetical order:

sort -r fruit.txt

The output is:

pear
banana
apple

The default order goes from apple to pear. Reverse order goes from the line that would appear later in the default order to the line that would appear earlier.

Check whether a file is already sorted

The -c option performs a sort check. It tests the file's order without printing a newly sorted version:

sort -c fruit.txt

Because fruit.txt is unordered, the command reports an ordering problem. The exact diagnostic can vary by Linux version, but it identifies the area or line where the expected order is first violated. The command also returns a nonzero exit status.

An exit status is a result code that a command or script can use to determine whether an operation succeeded. A successful check normally produces no ordinary output and returns a successful status.

Test a correctly ordered file such as sorted-fruit.txt:

apple
banana
pear
sort -c sorted-fruit.txt

There is normally no terminal output because the file passes the check, and the command exits successfully.

Using the check in a shell command

You can use the check result in a script or conditional command. For example:

if sort -c sorted-fruit.txt; then
    echo "File is sorted"
else
    echo "File is not sorted"
fi

For GNU/Linux sort, an unsorted input normally produces a nonzero status, while a successfully checked file produces status zero. A separate nonzero status can also indicate an actual command or file error, so scripts that need to distinguish every failure should consult man sort.

Core sort commands

Command or optionPurposeExpected behavior
sort FILESort lines in the default orderPrints the ordered lines to standard output and normally leaves the input file unchanged
sort -r FILEUse reverse orderingPrints lines from later to earlier in the default text order
sort -c FILECheck the existing orderNormally prints nothing when sorted; reports an ordering problem and returns a nonzero status when unsorted
man sortOpen the installed manual pageDisplays reference documentation for additional options and behavior

Important details about default ordering

Basic sort ordering is lexicographic, not necessarily the order a person expects from a dictionary or spreadsheet. Results can be affected by:

  • Uppercase and lowercase letters
  • Punctuation and spaces
  • The system's locale rules
  • Numbers stored as text

For this lesson, the example uses simple, consistently formatted lowercase words. If you sort names with mixed capitalization or records with punctuation, inspect the result rather than assuming it follows human dictionary rules.

Text order is not numeric order

When numbers are treated as text, character comparison can produce an order that differs from numerical value. For example, text sorting can place a line beginning with 10 before one beginning with 2, because the character 1 is compared before 2. Numeric sorting is an advanced use case; read the manual about options such as -n when numeric values are involved.

Read the built-in documentation

Open the installed manual page with:

man sort

The manual documents additional ordering modes, numeric comparisons, field or column selection, duplicate handling, locale-related behavior, and output controls. Use the arrow keys or Page Up and Page Down to read it, and press q to exit.

Troubleshooting

The file appears unchanged

This usually happens because sort fruit.txt prints sorted output to the terminal instead of editing the input file. Check the terminal output, then use redirection if you need a separate sorted file:

sort fruit.txt > sorted-fruit.txt

sort -c reports an ordering error

At least one line occurs after a line that should follow it under the active ordering rules. Inspect the reported line area, correct or reorder the input, and run the check again.

The order does not match expected dictionary order

Case, punctuation, and locale rules may affect the comparison. Try a consistently formatted input for basic learning, then consult man sort for alternative comparison options.

Numbers are not ordered by numeric value

The default comparison treats lines as text. If the lines contain numeric values, investigate numeric sorting options in the manual, including -n, before relying on the result.

Summary

  • sort FILE reads lines and prints them in default lexicographic order.
  • sort -r FILE prints the same lines in reverse order.
  • The input file is not changed merely because its contents were sorted for display.
  • sort -c FILE checks whether the file is already sorted and reports an ordering problem when it finds one.
  • man sort provides documentation for numeric sorting, field selection, duplicate handling, and other advanced features.

For related command-line work, you can also learn about sorting lines in a file as a starting point for exploring more advanced sort options.