How to Sort Lines in a Text File with the Linux sort Command
Learn how to sort Linux text files alphabetically, in reverse order, by a whitespace-separated field, and check whether a file is already sorted.
The Linux sort command reads text input and orders its lines. A line is a newline-delimited record in a text file. You can sort a file alphabetically, reverse the order, sort by a selected field, or check whether the file is already sorted.
This lesson assumes that you know how to run commands with filenames as arguments and that you are working with plain-text files.
What the sort command does
The basic syntax is:
sort FILE
For example, sort words.txt reads the lines in words.txt, orders them, and writes the result to standard output. Standard output is the normal destination for command results, usually the terminal.
sort words.txt
sort does not automatically change words.txt. It displays a sorted copy while leaving the source file unchanged.
Prepare and inspect an example file
Create a small file named words.txt with one word per line. The lines are deliberately not in alphabetical order.
zebra
apple
Orange
banana
kiwi
Use cat to inspect the file before sorting it:
cat words.txt
Example output:
zebra
apple
Orange
banana
kiwi
Keeping the original input visible makes it easier to compare the unsorted and sorted results. The mixed uppercase and lowercase example also illustrates that character ordering can depend on the current locale. For predictable beginner examples, use consistently cased text when possible.
Default alphabetical sorting
Run sort without options:
sort words.txt
A typical result for consistently lowercase input would be:
apple
banana
kiwi
zebra
With no field option, the complete line is the default sort key. A sort key is the line, or selected part of a line, used to determine ordering. Because each example line contains one word, the complete line is also the word.
This ordering is often called alphabetical order, but the more precise term is lexicographic order: text is compared according to its sequence of characters. The exact order of uppercase letters, lowercase letters, accented characters, and other characters can be affected by locale and character-collation rules.
Reverse sorting
Use the -r option to reverse the selected ordering:
sort -r words.txt
For lowercase input, the result is in reverse alphabetical order:
zebra
kiwi
banana
apple
Reverse order means the opposite of the normal ascending order. The option reverses whichever key would otherwise be used, including a field selected with -k.
Core sort command options
| Option or syntax | Purpose | Example |
|---|---|---|
sort FILE | Sort lines using the default ordering and the complete line as the key. | sort words.txt |
sort -r FILE | Reverse the sort order. | sort -r words.txt |
sort -k N FILE | Sort using field N. | sort -k 2 sort1.txt |
sort -c FILE | Check whether the input is already sorted. | sort -c words.txt |
Sort by a field or column
Many text files contain several whitespace-separated values on each line. A field is one portion of a line, and a field separator is what divides fields. For basic sort field selection, blank space is the default separator. Runs of whitespace are treated as the separation between fields.
Create a second example file named sort1.txt:
104 banana yellow
102 apple red
103 cherry dark-red
101 apple green
View the file:
cat sort1.txt
Fields are counted from 1, not 0:
| Original line | Field 1 | Field 2 | Role in sort -k 2 |
|---|---|---|---|
104 banana yellow | 104 | banana | banana is the sorting key |
102 apple red | 102 | apple | apple is the sorting key |
103 cherry dark-red | 103 | cherry | cherry is the sorting key |
101 apple green | 101 | apple | apple is the sorting key |
Sort by the second field with -k 2:
sort -k 2 sort1.txt
The lines are ordered by their second fields:
102 apple red
101 apple green
104 banana yellow
103 cherry dark-red
Notice that the two lines whose second field is apple remain next to each other. With the basic syntax -k 2, the selected key begins at field 2 and can extend through the remainder of the line. Therefore, text after the second field can influence the comparison when the second fields are equal. For precise key boundaries, GNU sort also supports forms such as -k 2,2, but the examples in this lesson use the required basic field selection.
To reverse a field-based sort, combine the options:
sort -r -k 2 sort1.txt
Check whether a file is already sorted
Use sort -c FILE to check the existing order without printing a sorted copy:
sort -c words.txt
If the file is correctly sorted according to the default comparison, the command normally produces no output and returns a successful exit status.
An exit status is a result code supplied by a command to the shell. A status of zero conventionally means success; a nonzero status indicates a problem or a negative check result. You can inspect the status immediately after running the command:
sort -c words.txt
echo $?
If an input file is not sorted, sort -c reports the first line it detects as out of order and returns a nonzero status. Compare that reported line with the line immediately before it to locate the first disorder.
zebra
apple
kiwi
sort -c words.txt
The exact diagnostic wording can vary by system, but it identifies the out-of-order line. The command checks the file; it does not rearrange it.
Keep sorted output in another file
Because normal output goes to the terminal, redirect it when you want to save a sorted copy:
sort words.txt > words-sorted.txt
The original words.txt remains unchanged, while words-sorted.txt receives the sorted output. GNU sort also provides an output-file option:
sort -o words-sorted.txt words.txt
Saving to a separate file is a safer beginner workflow than attempting to overwrite the input directly, because it lets you inspect the result before replacing the original.
Troubleshooting
The source file is still unsorted
This is expected when you run sort words.txt. The command writes the result to standard output by default. Use redirection or -o to save the result, preferably under a new filename first.
Sorting by the second field gives an unexpected order
Inspect the input with cat sort1.txt. Confirm that every line has the expected whitespace-separated layout and that the intended value is really field 2. Extra or inconsistent separators, missing fields, or a different field layout can change the result.
sort -c reports an out-of-order line
The reported line is the first detected violation of the expected ordering. Compare it with the preceding line. Remember that the check uses the default complete-line comparison unless you provide matching sort options.
Uppercase and lowercase appear in an unexpected order
Text comparison depends partly on the locale and its character-collation rules. Use consistently cased sample data when teaching or testing basic alphabetical sorting, and remember that “alphabetical” is a simplified description of a locale-sensitive text comparison.
Summary
sort FILEorders complete lines and prints the result to standard output.- The source file is not modified automatically.
-rreverses the selected ordering.-k 2selects the second whitespace-separated field as the beginning of the sort key.- Field numbering starts at 1.
sort -c FILEchecks ordering, prints no sorted copy, reports the first detected disorder, and returns a nonzero status when the file is not sorted.
For related command-line skills, see searching text with grep, showing the full path of shell commands, and the Linux command-line topics.