14 Useful Examples of Linux ‘sort’ Command – Part 1

Sort is a Linux program used for printing lines of input text files and concatenation of all files in sorted order. Sort command takes blank space as field separator and entire Input file as sort key. It is important to notice that sort command don’t actually sort the files but only print the sorted output, until your redirect the output.

This article aims at deep insight of Linux ‘sort‘ command with 14 useful practical examples that will show you how to use sort command in Linux.

1. First we will be creating a text file (tecmint.txt) to execute ‘sort‘ command examples. Our working directory is ‘/home/$USER/Desktop/tecmint.

The option ‘-e‘ in the below command enables interpretion of backslash and /n tells echo to write each string to a new line.

$ echo -e "computer\nmouse\nLAPTOP\ndata\nRedHat\nlaptop\ndebian\nlaptop" > tecmint.txt

Split String by Lines in Linux

2. Before we start with ‘sort‘ lets have a look at the contents of the file and the way it look.

$ cat tecmint.txt

Check Content of File

3. Now sort the content of the file using following command.

$ sort tecmint.txt

Sort Content of File linux

Note: The above command don’t actually sort the contents of text file but only show the sorted output on terminal.

4. Sort the contents of the file ‘tecmint.txt‘ and write it to a file called (sorted.txt) and verify the content by using cat command.

$ sort tecmint.txt > sorted.txt
$ cat sorted.txt

Sort File Content in Linux

5. Now sort the contents of text file ‘tecmint.txt‘ in reverse order by using ‘-r‘ switch and redirect output to a file ‘reversesorted.txt‘. Also check the content listing of the newly created file.

$ sort -r tecmint.txt > reversesorted.txt
$ cat reversesorted.txt

Sort Content By Reverse

6. We are going a create a new file (lsl.txt) at the same location for detailed examples and populate it using the output of ‘ls -l‘ for your home directory.

$ ls -l /home/$USER > /home/$USER/Desktop/tecmint/lsl.txt
$ cat lsl.txt

Populate Output of Home Directory

Now will see examples to sort the contents on the basis of other field and not the default initial characters.

7. Sort the contents of file ‘lsl.txt‘ on the basis of 2nd column (which represents number of symbolic links).

$ sort -nk2 lsl.txt

Note: The ‘-n‘ option in the above example sort the contents numerically. Option ‘-n‘ must be used when we wanted to sort a file on the basis of a column which contains numerical values.

Sort Content by Column

8. Sort the contents of file ‘lsl.txt‘ on the basis of 9th column (which is the name of the files and folders and is non-numeric).

$ sort -k9 lsl.txt

Sort Content Based on Column

9. It is not always essential to run sort command on a file. We can pipeline it directly on the terminal with actual command.

$ ls -l /home/$USER | sort -nk5

Sort Content Using Pipe Option

10. Sort and remove duplicates from the text file tecmint.txt. Check if the duplicate has been removed or not.

$ cat tecmint.txt
$ sort -u tecmint.txt

Sort and Remove Duplicates

Rules so far (what we have observed):

  1. Lines starting with numbers are preferred in the list and lies at the top until otherwise specified (-r).
  2. Lines starting with lowercase letters are preferred in the list and lies at the top until otherwise specified (-r).
  3. Contents are listed on the basis of occurrence of alphabets in dictionary until otherwise specified (-r).
  4. Sort command by default treat each line as string and then sort it depending upon dictionary occurrence of alphabets (Numeric preferred; see rule – 1) until otherwise specified.

11. Create a third file ‘lsla.txt‘ at the current location and populate it with the output of ‘ls -lA‘ command.

$ ls -lA /home/$USER > /home/$USER/Desktop/tecmint/lsla.txt
$ cat lsla.txt

Populate Output With Hidden Files

Those having understanding of ‘ls‘ command knows that ‘ls -lA’=’ls -l‘ + Hidden files. So most of the contents on these two files would be same.

12. Sort the contents of two files on standard output in one go.

$ sort lsl.txt lsla.txt

Sort Contents of Two Files

Notice the repetition of files and folders.

13. Now we can see how to sort, merge and remove duplicates from these two files.

$ sort -u lsl.txt lsla.txt

 Sort, Merge and Remove Duplicates from File

Notice that duplicates has been omitted from the output. Also, you can write the output to a new file by redirecting the output to a file.

We may also sort the contents of a file or the output based upon more than one column. Sort the output of ‘ls -l‘ command on the basis of field 2,5 (Numeric) and 9 (Non-Numeric).

$ ls -l /home/$USER | sort -t "," -nk2,5 -k9

Sort Content By Field Column

That’s all for now. In the next article we will cover a few more examples of ‘sort‘ command in detail for you. Till then stay tuned and connected to Tecmint. Keep sharing. Keep commenting. Like and share us and help us get spread.

Read Also: 7 Interesting Linux ‘sort’ Command Examples – Part 2

Tutorial Feedback...
Was this article helpful? If you don't find this article helpful or found some outdated info, issue or a typo, do post your valuable feedback or suggestions in the comments to help improve this article...

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

7 thoughts on “14 Useful Examples of Linux ‘sort’ Command – Part 1”

  1. 1. A key specification like -k2 means to take all the fields from 2 to the end of the line into account. To sort by a single column, use -k2,2 as the key specification. This means to use the fields from #2 to #2, i.e. only the second field.

    2. While doing something like “sort -nk5,5 table.txt“, why does the sort command remove entries with 0? (have files with size 0-should get sorted not removed)

  2. The link to 7 Interesting Sort Commands does not work properly. It loads this article

    There are only 10 ‘sort’ command examples. The other four entries just create files to be input to the ‘sort’ command examples.

  3. Hi!
    Just wondering why you type ”/home/$USER/” instead of just ”$HOME/”. Unnecessary typing. Just saying. :) Or am I missing something?

  4. I have a copy of a 4.09 GB wordlist / dictionary, crackstation.txt. Just now the words and numbers are arranged in the usual order: 0123456789aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStT uUvVwWxXyYzZ

    How do I rearrange the order to something like:

    tTaAsShHwWiIoObBmMfFcCdDpPnNeEgGrRyYuUvVjJkKqQzZxX 1023985467

    (This sequence gives the first letters of English words in order of popularity, from most popular (left) to least popular (right))

  5. “14 Useful Examples…”

    No, it’s 14 numbered entries, but several are just duplicate functions of sort using files vs using standard input.


Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.