Sort lines in a file

You can use the sort command in the terminal in Raspbian to sort the lines of a text file. This command can be useful if you have a file with unordered lines and want to sort the lines in some order, for example in alphabetical order.

Here’s our example text file:

sort example

We can use the sort command without any options to sort the lines in the file in the alphabetical order:

sort command

To sort the lines in reverse alphabetical order, we can use the -r flag with the sort command:

sort in reverse command

To check if the lines in the file are already sorted in alphabetical order, we use sort with the -c flag. This command will report the first unsorted line:

sort check command

To get even more information about the sort command, check the command’s man pages with the man sort command.
Geek University 2021