Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
ls -al | sort -k5 works for me, could you edit in the output of ls -al –  Nifle Oct 7 '10 at 13:03
    
Okay, I made the output complete. Thanks for your response. –  mindy Oct 7 '10 at 13:34

2 Answers 2

up vote 6 down vote accepted

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

share|improve this answer
    
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 Oct 7 '10 at 14:04
    
@mindy : no clue why -n would not work for you. Try again with -nk6? :-) –  Martin Oct 7 '10 at 14:20
    
@Martin: You missed sort in your examples (ls -al | -nk5, etc.). –  cYrus Oct 7 '10 at 14:26
    
@Martin: Also the last one doesn't work as you don't specify the parameter for k. –  cYrus Oct 7 '10 at 14:29
    
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 Oct 7 '10 at 14:43

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

share|improve this answer
    
Thanks, but since I specified that my goal was to learn piping, this doesn't answer the question. –  mindy Oct 8 '10 at 0:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.