Linux sort Command Tutorial for Beginners (8 Examples)

Looking for a command line utility to sort content in text files? Look no further than Sort, a tool specifically built for this purpose. In this tutorial, we will discuss this command using some easy-to-understand examples. But before we do that, it's worth mentioning that all examples here have been tested on Ubuntu 22.04 LTS machine.

Linux Sort command

The Sort command allows you to sort lines in a text file. Following is its syntax:

sort [OPTION]... [FILE]...

And here's how the tool's man page describes it:

Write sorted concatenation of all FILE(s) to standard output.
With no FILE, or when FILE is -, read standard input.

Following are some Q&A-styled examples that should give you a good idea of how the sort command works.

Q1. How to use sort command?

Suppose you have a file that contains some names, and you want to sort those in alphabetical order. Then all you need to do is to pass the name of the file as input to the sort command.

For example:

sort file1

So if file 1 contained the following lines:

Zimbabwe
Serbia
Norway
Australia

Then the output would be:

Australia
Norway
Serbia
Zimbabwe

Here an example:

Sort file on Linux

Q2. How to make sort ignore leading blanks?

Depending upon your locale, you may see sort producing unexpected results when lines contain leading blanks. For example:

Suppose file contains following lines:

Zimbabwe
 Serbia
  Norway
Australia

And you run sort, only to see the following result:

  Norway
 Serbia
Australia
Zimbabwe

This may look unexpected, but what actually happened is that lines that contain leading blanks were sorted on the basis of blanks, while others were sorted alphabetically. To make sure the Sort command ignores leading blanks, use the -b option. So in that case, you'll get the following result:

Australia
  Norway
 Serbia
Zimbabwe

Q3. How to make sort ignore case?

If a file has words/lines beginning with both upper case and lower case characters, then sort displays those with upper case at top. However, if you want, you can change this behavior using the -f command line option.

For example:

sort -f file1

Q4. How to make sort compare numbers?

Suppose a file only contains numbers, and you want sort to order them. Then this can be made possible using the -g command line option.

sort -g file1

For example a file with following contents:

32000
2500
50000
54

Can be sorted using the sort command to produce following results:

54
2500
32000
50000

Q5. How to make sort work with human readable numeric values?

In case you want sort to work with human readable numeric values like 1K, 2G, etc, use the -h command line option.

sort -h file1

So, for example, a file with the following lines:

1M
2G
3K

Can be sorted in the following way using the -h option:

3K
1M
2G

Q6. How to make sort only check for sorted input?

Just in case you want sort to only check if a file is sorted or not, use the -c command line option.

sort -c file1

For example, if file1 contains the following lines:

dhg
lkh
zyb
abd

Then using -c will see sort producing the following output:

sort: file1:4: disorder: abd

So you observe that the tool not only points there's a disorder but outputs its location as well.

Q7. How to make sort merge already sorted files?

If you want sort to merge two already sorted files, then use the -m command line option.

sort -m file1 file2

For example, both file1 and file2 contained the following lines in my case:

abd
dhg
lkh
zyb

And here's how the -m option merged these files:

abd
abd
dhg
dhg
lkh
lkh
zyb
zyb

Q8. How to make sort write the result to a file?

By default, the sort command writes output to STDOUT. However, you can force it to write to a given file using the -o option.

For example:

sort file1 -o output.txt

Conclusion

The Sort command provides a lot of options. We have discussed a few key ones here. We suggest you practice these first, and once you are done, head to the command's man page to learn more about it.

Share this page:

0 Comment(s)