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

6

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?

mindy

Posted 2010-10-07T12:50:42.870

Reputation: 63

ls -al | sort -k5 works for me, could you edit in the output of ls -al – Nifle – 2010-10-07T13:03:46.980

Okay, I made the output complete. Thanks for your response. – mindy – 2010-10-07T13:34:25.333

Answers

8

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

Martin

Posted 2010-10-07T12:50:42.870

Reputation: 2 055

Thanks, Martin. Is that different from the -nk5 that I already tried? I just tried putting -n into different places in the command, but that doesn't seem to make a difference. – mindy – 2010-10-07T14:04:34.747

@mindy : no clue why -n would not work for you. Try again with -nk6? :-) – Martin – 2010-10-07T14:20:32.850

@Martin: You missed sort in your examples (ls -al | -nk5, etc.). – cYrus – 2010-10-07T14:26:05.027

@Martin: Also the last one doesn't work as you don't specify the parameter for k. – cYrus – 2010-10-07T14:29:27.090

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 – mindy – 2010-10-07T14:43:57.803

@cYrus : Corrected the messy examples :-) – Martin – 2010-10-07T15:32:57.827

4

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

BillThor

Posted 2010-10-07T12:50:42.870

Reputation: 9 384

Thanks, but since I specified that my goal was to learn piping, this doesn't answer the question. – mindy – 2010-10-08T00:34:18.903