How to sort ls by column in Ubuntu via piping to sort?

12,847

Solution 1

The output you show is sorted alphabetically instead of numerically. Try adding:

-n, --numeric-sort compare according to string numerical value

Edit: I just noticed that in your output the file size column appears to be the 6th one! have you tried -nk6?

Tested on my Hardy Heron, these work:

  • ls -al | sort -nk5
  • ls -al | sort -k5n
  • ls -al | sort -k5 -n
  • ls -al | sort -k5 --numeric-sort

sort --version yields: 6.10

Solution 2

To sort by size try 'ls -S' or 'ls -rS'.

Share:
12,847

Related videos on Youtube

mindy
Author by

mindy

Updated on September 17, 2022

Comments

  • mindy
    mindy almost 2 years

    I'm a Linux newbie trying to learn how piping works in the Ubuntu terminal. I tried to sort by file size:

    ls -al | sort -k5
    

    The files were listed in this order:

    drwx------ 19 min10 domain users      0 2010-10-07 12:38 .
    drwx------ 29 min10 domain users      0 2010-02-16 22:30 MSI
    drwx------  2 min10 domain users      0 2009-11-25 19:53 www
    drwx------  2 min10 domain users      0 2010-02-05 09:14 .cache
    drwx------  2 min10 domain users      0 2010-03-02 08:11 My Pictures
    drwx------  2 min10 domain users      0 2010-03-02 10:41 regu
    drwx------  2 min10 domain users      0 2010-03-31 13:08 elec
    drwx------  2 min10 domain users      0 2010-04-10 21:43 temp
    drwx------  2 min10 domain users      0 2010-10-06 09:13 bubbla
    drwx------  3 min10 domain users      0 2010-01-26 08:26 cfg
    drwx------  3 min10 domain users      0 2010-03-30 15:34 data
    drwx------  3 min10 domain users      0 2010-03-30 16:03 idv
    drwx------  3 min10 domain users      0 2010-04-01 09:10 arduino-0018
    drwx------  3 min10 domain users      0 2010-04-14 15:10 processing-1.1
    drwx------  3 min10 domain users      0 2010-07-06 16:20 eclipse
    drwx------  4 min10 domain users      0 2010-04-15 09:34 pryl
    drwx------  4 min10 domain users      0 2010-07-06 16:30 dv2
    -rwx------  1 min10 domain users    123 2010-10-07 12:38 starwars.txt
    -rwx------  1 min10 domain users  16109 2010-10-06 07:01 .bash_history
    -rwx------  1 min10 domain users 454656 2010-03-30 15:29 putty.exe
    -rwx------  1 min10 domain users    504 2010-04-10 21:16 little.gif
    -rwx------  1 min10 domain users  56682 2010-04-10 21:12 awesome.jpg
    -rwx------  1 min10 domain users      7 2010-04-10 21:32 blah.txt
    

    This is obviously a fail in terms of sorting by file size. I tried with a bunch of other columns in addition to -k5, and repeatedly get muddled, out of order, results. I also tried -nk5, and still no go. What am I doing wrong?

    • Admin
      Admin over 13 years
      ls -al | sort -k5 works for me, could you edit in the output of ls -al
    • Admin
      Admin over 13 years
      Okay, I made the output complete. Thanks for your response.
  • Martin
    Martin over 13 years
    @mindy : no clue why -n would not work for you. Try again with -nk6? :-)
  • cYrus
    cYrus over 13 years
    @Martin: Also the last one doesn't work as you don't specify the parameter for k.
  • mindy
    mindy over 13 years
    Ah, you were right. I had tried -n and I had tried -k6, but I hadn't tried them both together. Now it works. I'm confused why it's the sixth column, though. I'm counting five. 1) Permissions 2) Some number 3) Owner name 4) Group name 5) File size
  • Martin
    Martin over 13 years
    @cYrus : Corrected the messy examples :-)
  • mindy
    mindy over 13 years
    Thanks, but since I specified that my goal was to learn piping, this doesn't answer the question.