The Linux sort command is a useful tool for sorting and organizing data effectively. It can help you manage both simple text files and complex datasets, improving your productivity.
When you have a large number of files, server logs, or a massive CSV file that is all jumbled up, it can be overwhelming. You want to organize it, but everything is out of order. This is where the Linux sort command becomes your helpful tool. Whether you’re a beginner learning Linux, a sysadmin managing log files, or a developer handling data, knowing how to use the sort command can save you a lot of time and hassle.
In this tutorial, you’ll learn not only what the sort command does but also how to use it effectively with practical examples you can try in your terminal.
Why Learn The Sort Command in Linux?
The sort command in Linux arranges the output of a file in a specific order. It processes data from a file or command output and organizes it for easier reading. This is especially helpful when dealing with large amounts of information that need to be sorted alphabetically or numerically, either in ascending or descending order.
The command sorts text file contents line by line and is a standard tool for displaying sorted lines from input or multiple files. It can sort alphabetically, in reverse, by number, by month, and can eliminate duplicates.
Additionally, it can sort items based on items not at the start of a line, ignore cases, and check if a file is already sorted.
Get exclusive access to all things tech-savvy, and be the first to receive
the latest updates directly in your inbox.
By default, it uses the entire input as the sort key, with blank spaces as the field separator. The sort command has specific rules: lines starting with numbers come before those starting with letters, letters earlier in the alphabet come before those later in the alphabet, and uppercase letters precede their lowercase counterparts.

Basic Syntax Of The Sort Command
The sort command is used like this:
sort [options] filename.
It organizes data in a certain order, while the grep command shows or hides specific information.
If you run the sort command without any options, it displays the file’s contents using the default sorting rules for your locale.
The basic rules are: lines that start with numbers come before those that start with letters, sorted from 1 to 10, and lines that start with letters are sorted alphabetically from A to Z.
1. Sorting Alphabetically (Default Behavior)
2. Sorting with Delimiters
When words are separated by a space, the ‘sort’ command sees anything after a space as a new word. But what if the separator is different?
For example, if a text file uses symbols like ‘|’ or ‘+’ or ‘.’, you can create a file with ‘+’ as the separator.
Use the cat command to view the file’s contents. To create the file, run:
echo -e "21+linux+server+production\n11+debian+RedHat+CentOS\n131+Apache+Mysql+PHP\n7+Shell Scripting+python+perl\n111+postfix+exim+sendmail" > delimiter.txt
And then check it with:
$ cat delimiter.txt.
3. Organizing File Content by Month
The Linux sort command below arranges the data in ‘month.txt’ by month in order, using the ‘-M’ option to tell the ‘sort’ command to recognize the entries as dates and sort them properly. Run this command:

sort -M month.txt
4. Sorting by Numbers
Sort by numerical value using option -n. Let’s arrange a list in numerical order. This list shows the most popular Linux distributions as of July 2019. I will change the file contents to number the items, but they will be out of order as follows: 1. MX Linux, 4. elementary, 2. Manjaro, 5. Ubuntu, 3. Mint.
When I run the command
'sort filename.txt',
The sorted result will be:
1. MX Linux<br>
2. Manjaro<br>
3. Mint<br>
4. elementary<br>
5. Ubuntu.
This looks good, but can you trust this method for accurate data arrangement? Not really.
Let’s look at another example to understand why. Here’s my new sample text:
1, 5, 10, 3, 5, 2, 60, 23, 432, 21.
If I use the sort command without options, the output will be:
1, 10, 2, 21, 23, 3, 432, 5, 5, 60.
The numbers are sorted based on their first characters only. However, when I add the -n option, the sort command evaluates the numerical value of the strings instead of just the first character. The correctly sorted output will then be:
1
2
3
5
5
10
21
23
60
432
5. Sorting by Specific Columns
You can sort a table in your file using the -k option to choose the column. I added some random numbers as a third column and will show the output sorted by each column. I included examples to demonstrate the different possible outputs. Options are added after the column number.
1. MX Linux 100
2. Manjaro 400
3. Mint 300
4. elementary 500
5. Ubuntu 200
sort filename.txt -k 2
This will sort the text on the second column in alphabetical order:
4. elementary 500
2. Manjaro 400
3. Mint 300
1. MX Linux 100
5. Ubuntu 200
sort filename.txt -k 3n
This will sort the text by the numerals on the third column.
1. MX Linux 100
5. Ubuntu 200
3. Mint 300
2. Manjaro 400
4. elementary 500
sort filename.txt -k 3nr
Same as the above command, just that the sort order has been reversed.
4. elementary 500
2. Manjaro 400
3. Mint 300
5. Ubuntu 200
1. MX Linux 100
6. Sort Lines in Reverse Order in a Linux File
To sort lines in reverse, use the ‘-r’ option. For example, to sort the lines in a file named ‘example.txt’ in reverse order, you can use the command:
First, display the file’s content with:
cat example.txt.
sort -r example.txt.
Store the sorted results in a different file.
As stated before, sorting does not alter the original file by default. If you want to keep the sorted data, you can do so. In this case,create a new file named filename_sorted.txt to save the sorted information. Be careful: directing sorted data to the same file will overwrite its contents.
Use the command
sort filename.txt -n > filename_sorted.txt.
Linux Sort Command Cheat Sheet

End Note
Sorting stuff is a boring task; that’s why a sort command in Linux is essential. It’s a skill that can make you a wizard in the terminal, making everything faster and more efficient for you. In this article, I discussed 7 Linux Sort Command in detail, and
With a Sort command, you can turn chaos in clarity!
FAQ’s
1. What is the Linux sort command? The Linux sort command is used to arrange lines in text files in a specific order—whether alphabetically, numerically, or based on certain fields. This is essential for organizing large datasets or log files.
2. How can I sort a file by numbers in Linux?
You can sort a file by numbers with:
<br>sort -n filename.txt
This keeps numbers such as 2 in front of 10 instead of being alphabetically ordered
3. How do I sort a CSV file by a particular column?
Use -t to specify the delimiter and -k to select the column:
sort -t',' -k2 file.csv
This sorts the file on the second column of your CSV.
4. What’s the difference between sort and sort-u in Linux?
Whereas sort puts lines in order, sort-u does the same but eliminates duplicate lines, great for tidying up data.
5. Can I sort version numbers properly with sort?
Yes! Use the -V flag to sort software version numbers such as v2.1, v10.4, etc., in proper logical order:
sort -V versions.txt