Sort Lines in a Text File on Raspberry Pi
Learn how to use sort on Raspberry Pi OS to alphabetize text-file lines, reverse the order, check whether a file is sorted, and read the built-in manual.
On Raspberry Pi OS (formerly called Raspbian), the sort command orders lines in a text file. It is useful for alphabetizing names, arranging lists, and making unordered data easier to read.
A text file contains readable characters. When its content is organized one item per line, each newline-delimited line is treated as one sortable record. By default, sort orders complete lines using ascending lexical, or alphabetical, order according to the active locale.
The basic command prints the sorted result in the terminal. It does not change the original file.
Before you begin
You should know how to open a terminal, run commands, navigate with cd, and list files with ls. If you need a refresher, see Terminal in Raspbian and Useful Terminal Commands.
Prepare an unordered example file
Create a small file named animals.txt. The values are intentionally not in alphabetical order:
cat > animals.txt <<'EOF'
zebra
ant
lemur
EOF
Inspect the original file with cat:
cat animals.txt
zebra
ant
lemur
The file contains one animal per line, but the sequence is zebra, ant, lemur, not alphabetical order.
Sort lines in ascending alphabetical order
The basic syntax is:
sort FILE
Run it with the example file:
sort animals.txt
The terminal displays:
ant
lemur
zebra
This is ascending alphabetical order. The command reads the lines from animals.txt, orders them, and writes the result to standard output, which is normally the terminal.
Sort lines in reverse order
Use the -r option to request reverse order. This reverses the selected sort order, so the default ascending order becomes descending:
sort -r animals.txt
Output:
zebra
lemur
ant
The -r option affects the displayed ordering only; it still does not modify the source file.
Check whether a file is already sorted
The -c option puts sort into check mode. Instead of printing a sorted copy, it tests whether the input is already in the expected ascending order:
sort -c animals.txt
Because animals.txt is not sorted, the command prints a diagnostic identifying the first line that breaks the order. The exact wording can vary with the installed version, but it will identify the file and a disorder position, similar to:
sort: animals.txt:2: disorder: ant
The command also returns a nonzero exit status. An exit status is the success or failure code returned by a command, and scripts can use it to decide what to do next.
To check a correctly ordered file, first save sorted output in a separate file:
sort animals.txt > sorted-animals.txt
sort sorted-animals.txt -c
A correctly sorted file produces no normal diagnostic. The command indicates success through its exit status. To display that status immediately afterward, run:
echo $?
A result of 0 means the check succeeded. A nonzero result means the file failed the check.
| Goal | Command | Behavior |
|---|---|---|
| Sort lines ascending | sort FILE | Prints lines in default ascending order |
| Sort lines descending | sort -r FILE | Prints lines in reverse order |
| Check ascending order | sort -c FILE | Verifies order and reports the first ordering problem |
| Open documentation | man sort | Shows the command manual |
Read the local sort documentation
Linux commands include local reference documentation called man pages. A man page is opened with the man utility. Read the complete reference for sort with:
man sort
Use the arrow keys or Page Up and Page Down to read. Press q to quit. The manual describes additional flags, ordering behavior, input handling, and usage details.
Troubleshoot common problems
The file still looks unsorted
Normal sorting sends its result to the terminal and leaves the input file unchanged. Review the displayed output, or save it separately with redirection:
sort animals.txt > sorted-animals.txt
This creates a new file rather than replacing the original.
sort -c reports an ordering error
At least one line appears before a line that should precede it in ascending order. Inspect the reported line and the lines around it. You can correct the file manually or generate an ordered version with sort.
sort -c produces no visible text
No diagnostic is the expected result when the file passes the check. Run echo $? immediately afterward to see the exit status.
Uppercase letters, accents, or punctuation sort unexpectedly
Text comparison can depend on the active locale and its character-collation rules. Simple lowercase examples avoid most of this complexity. If your data contains mixed case, accented characters, or punctuation, consult man sort for relevant options and locale behavior.
The terminal cannot open the file
An error such as “No such file or directory” usually means the filename, path, or current working directory is wrong. Check your location and available files:
pwd
ls
Change to the directory containing the file with cd, or provide the file's correct path when running sort.
Key points
sorttreats each newline-delimited line as one sortable unit.sort FILEprints ascending lexical order without changing the source file.sort -r FILEprints the lines in descending order.sort -c FILEchecks ascending order, reports the first disorder, and uses its exit status to indicate success or failure.man sortopens the built-in reference for more options.