≡ Menu

How to Sort Files in Linux using Sort Command

Sort command is helpful to sort/order lines in text files. You can sort the data in text file and display the output on the screen, or redirect it to a file. Based on your requirement, sort provides several command line options for sorting data in a text file.

Sort Command Syntax:

$ sort [-options]

For example, here is a test file:

$ cat test
zzz
sss
qqq
aaa
BBB
ddd
AAA

And, here is what you get when sort command is executed on this file without any option. It sorts lines in test file and displays sorted output.

$ sort test
aaa
AAA
BBB
ddd
qqq
sss
zzz

1. Perform Numeric Sort using -n option

If we want to sort on numeric value, then we can use -n or –numeric-sort option.

Create the following test file for this example:

$ cat test
22 zzz
33 sss
11 qqq
77 aaa
55 BBB

The following sort command sorts lines in test file on numeric value in first word of line and displays sorted output.

$ sort -n test
11 qqq
22 zzz
33 sss
55 BBB
77 aaa

2. Sort Human Readable Numbers using -h option

If we want to sort on human readable numbers (e.g., 2K 1M 1G), then we can use -h or –human-numeric-sort option.

Create the following test file for this example:

$ cat test
2K
2G
1K
6T
1T
1G
2M

The following sort command sorts human readable numbers (i.e 1K = 1 Thousand, 1M = 1 Million, 1G = 1 Giga, 1T = 1 Tera) in test file and displays sorted output.

$ sort -h test
1K
2K
2M
1G
2G
1T
6T

3. Sort Months of an Year using -M option

If we want to sort in the order of months of year, then we can use -M or –month-sort option.

Create the following test file for this example:

$ cat test
sept
aug
jan
oct
apr
feb
mar11

The following sort command sorts lines in test file as per month order. Note, lines in file should contain at least 3 character name of month name at start of line (e.g. jan, feb, mar). If we will give, ja for January or au for August, then sort command would not consider it as month name.

$ sort -M test
jan
feb
mar11
apr
aug
sept
oct

4. Check if Content is Already Sorted using -c option

If we want to check data in text file is sorted or not, then we can use -c or –check, –check=diagnose-first option.

Create the following test file for this example:

$ cat test
2
5
1
6

The following sort command checks whether text file data is sorted or not. If it is not, then it shows first occurrence with line number and disordered value.

$ sort -c test
sort: test:3: disorder: 1

5. Reverse the Output and Check for Uniqueness using -r and -u options

If we want to get sorted output in reverse order, then we can use -r or –reverse option. If file contains duplicate lines, then to get unique lines in sorted output, “-u” option can be used.

Create the following test file for this example:

$ cat test
5
2
2
1
4
4

The following sort command sorts lines in test file in reverse order and displays sorted output.

$ sort -r test
5
4
4
2
2
1

The following sort command sorts lines in test file in reverse order and removes duplicate lines from sorted output.

$ sort -r -u test
5
4
2
1

6. Selectively Sort the Content, Customize delimiter, Write output to a file using  -k, -t, -o options

If we want to sort on the column or word position in lines of text file, then “-k” option can be used. If we each word in each line of file is separated by delimiter except ‘space’, then we can specify delimiter using “-t” option. We can get sorted output in any specified output file (using “-o” option) instead of displaying output on standard output.

Create the following test file for this example:

$ cat test
aa aa zz
aa aa ff
aa aa tt
aa aa kk

The following sort command sorts lines in test file on the 3rd word of each line and displays sorted output.

$ sort -k3 test
aa aa ff
aa aa kk
aa aa tt
aa aa zz
$ cat test
aa|5a|zz
aa|2a|ff
aa|1a|tt
aa|3a|kk

Here, several options are used altogether. In test file, words in each line are separated by delimiter ‘|’. It sorts lines in test file on the 2nd word of each line on the basis of numeric value and stores sorted output into specified output file.

$ sort -n -t'|' -k2 test -o outfile

The contents of output file are shown below.

$ cat outfile
aa|1a|tt
aa|2a|ff
aa|3a|kk
aa|5a|zz

Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • Bob April 9, 2013, 8:32 am

    Great article.. Thanks!!!!

  • Jalal Hajigholamali April 9, 2013, 8:45 am

    Hi,
    Thanks a lot..
    Easy but very useful article

  • vern April 9, 2013, 9:08 am

    in the last example – $sort -n -t’|’ -k2 test -o outfile – – – i changed ‘-k2’ to ‘-k3’ and expected to see:
    aa|2a|ff
    aa|3a|kk
    aa|1a|tt
    aa|5a|zz

    and changed the output from ‘outfile’ to ‘outfile1’ but the results were the same.

    also ‘-h’ did not work for me.

  • OP Singh April 9, 2013, 11:21 am

    Really your articles are very useful and simple. I am learning linux from your commands. Thanks please maintain it.

  • Chris April 9, 2013, 12:02 pm

    THANKS for your intereting artical.
    Very useful skills to know, which is simple an easy to understand.
    I will play little with this commands to learn and comfortable.
    //Chris

  • Philippe Petrinko April 9, 2013, 2:00 pm

    Hi Himanshu,
    Thanks for this topic.

    May I know the detailed reason why lowercase prededes uppercase, as in:
    $ sort test
    aaa
    AAA
    BBB
    ddd
    qqq
    sss
    zzz

    As long as I remember, from ASCII to UTF-8, uppercase has smaller code value than lowercase, so how come lowercase comes first?

    — Philippe

  • Philippe Petrinko April 9, 2013, 2:37 pm

    Ok, got it. It depends on [locale] settings, ( on LC_COLLATE )
    for instance:

    LC_COLLATE=C sort data
    AAA
    BBB
    SSS
    aaa
    ddd
    qqq
    sss

    – -Philippe

  • Chris F.A. Johnson April 9, 2013, 4:06 pm

    You have left out multiple fields, field ranges and character selection with -k, e.g. -k1,2 -k1.9,1.11n

  • Chris F.A. Johnson April 9, 2013, 4:07 pm

    Vern, remove -n from your command. With it, you are sorting numerically, and, since there are no digits, all the fields evaluate to 0.

  • Juan April 9, 2013, 4:22 pm

    Vern, you couldn’t change the order because you are still using the flag -n (which is the numeric sort).
    Remove that and I will work.

    Cheers
    Juan

  • MK April 9, 2013, 9:17 pm

    Thanks a lot.
    If you could include sort by column, that would be great
    example sort -k1.5,1.10 file > sortfile

  • solamour April 9, 2013, 9:56 pm

    As for “-h”, I think that’s a relatively recent feature, so older version of sort doesn’t have it. My system has sort version 8.20, which does have “-h” option.
    __
    sol

  • Ethan April 12, 2013, 6:43 am

    Great article.

  • isomorphismes April 15, 2013, 7:31 pm

    sort — human-numeric-test doesn’t seem to exist on Mac OS X.

  • Prasanth M April 15, 2013, 10:36 pm

    Your explanation for every command is very simple and easy to understand. Thanks!!

  • Sysadmn April 30, 2013, 8:26 am

    Two of my favorite sort tricks:

    1) sort -bdfu : sort, ignoring leading blanks and any non-alphanumeric characters, treating upper and lower case the same; keep only one occurrence of identical lines.

    2) sort -t. -k 1n -k 2n -k 3n -k 4n

    Sort a list of IPv4 addresses (such as /etc/hosts) in numerical order.

    Example:
    192.168.10.1 gateway-10
    192.168.1.2 router-2
    192.168.20.1 server-20-1
    192.168.2.2 server-2-2
    192.168.10.2 gateway-10-alt
    192.168.1.1 router-1

    After “sort”, you get a strange order, since “0” comes before “.”.
    192.168.10.1 gateway-10
    192.168.10.2 gateway-10-alt
    192.168.1.1 router-1
    192.168.1.2 router-2
    192.168.20.1 server-20-1
    192.168.2.2 server-2-2

    After sort -t. -k 1n -k 2n -k 3n -k 4n
    192.168.1.1 router-1
    192.168.1.2 router-2
    192.168.10.1 gateway-10
    192.168.10.2 gateway-10-alt
    192.168.2.2 server-2-2
    192.168.20.1 server-20-1

    Even better, these commands can be used inside vi/vim. Put the cursor on the first line you want to sort, and type “!}sort -bdfu”. When you hit enter, all lines up to the next blank line are passed to the sort command, and inserted in place of the original.

  • VIVEK July 31, 2013, 8:19 pm

    cat > file
    dD
    Dc..

    I expected the o/p aftr SORT FILE to stay same as i/p…Bt I gt it as
    Dc
    dD..Generally , wen I try to sort 2 letters d and D, d is displayed 1st followed by D..
    M confused about wat format is followed fr sorting(ascii ??)..Can some 1 pls help me with this?

  • Chris F.A. Johnson July 31, 2013, 8:35 pm

    VIVEK, sort will use the collating order of your locale.

    This may be aAbBcCdD… or AaBbCcDd… or, in the POSIX/C locale which is recommended for scripting, ABCD….abcd.

    The best thing to use is: export LC_ALL=C

  • anar December 24, 2013, 11:45 pm

    does anyone know hot to sort selected lines like for example i have 50 lines and i only need to see first 10 lines. is it possible??? pls help me

  • Chris F.A. Johnson December 27, 2013, 4:41 pm

    Anar, use head:

    head “$file” | sort

    Or, if you want to sort the first ten lines and keep the rest as they are:
    {
    head | sort
    cat
    } < "$file"

  • Sumesh March 13, 2014, 6:00 am

    Hi,

    I’m trying to sort some data into a specific order. For example I have the following

    21
    21A
    21AB
    CD
    25
    25ZC
    XY
    45A

    What I actually want is this :

    21A
    21AB
    25ZC
    45A

  • Philippe Petrinko March 13, 2014, 10:41 am

    @Sumesh

    So, what did you try, and what were the results?

    By the way, you did not explained why following values (21, CD, XY, 25) are missing in you expected output list?

  • Sumesh March 13, 2014, 10:07 pm

    Hi,

    I need to display only those values which is the combination of Alpha-Numeric.

    And remove the values which is only alphabet or numeric values.

    Hope this will clear.

    Thanks,
    Sumesh.

  • Philippe Petrinko March 14, 2014, 5:34 am

    That’s clearer.
    So, you need not only to sort, but also to filter data.

    To filer, you could use [grep] command, and to filter you could use [sort] command
    And you could use a “pipe” | between these two commands.

    STFW (Search The Fantastic Web) and you will find what you need.
    When you will have tried something, show us, in order to help you after you helped you first. 😉

    –P

  • Sumesh March 18, 2014, 11:35 pm

    Help me the command as you mentioned, becuase I am new to unix.
    Help me for writing this type of command. Conside this values are availble in “test.txt” file.

  • jega September 21, 2015, 8:16 pm

    Write a bash script that takes one or more file names on the command line and prints out:
    1. The name of the file
    2. The number of lines & words in the file (NOT characters)
    3. A list of all the words in the file and how many times they occur
    (ideally, the words should all be lower cased)

    Hint: you you will probably need to learn about pipe, loops, variables, echo, wc, cat, tr, sort, & uniq.

  • Pavlin Georgiev October 31, 2015, 12:26 am

    We can add commands to sort a list of IP addresses in a file:
    cat ip_list.txt | sort -t”.” -k1n -k2n -k3n -k4n