Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to take this command find -maxdepth 1 -type d | while read -r dir; do printf "%s:\t" "$dir"; find "$dir" | wc -l; done ( from here ). which has an output of basically

./kennel:       11062
./shadow:       15449
./ccc:  9765
./journeyo:     14200
./norths:       10710

and sort it by the numbers largest to smallest. but I'm not sure how to make sort, or whatever operate on a different column.

share|improve this question
up vote 18 down vote accepted

Pipe the lines through sort -n -r -k2. Edited to sort from largest to smallest.

share|improve this answer
    
Tried it and works flawlessly – Gert Nov 18 '10 at 13:46

One option is to flip the columns:

$ find -maxdepth 1 -type d | while read -r dir; do printf "%d\t%s\n" "`find "$dir" | wc -l`" "$dir"; done

Then you get output like this:

17  .
1   ./acroread_1000_1002
1   ./.ICE-unix
2   ./.X11-unix
1   ./orbit-mrozekma
2   ./ns.mrozekma.:0

You can pipe that through sort -nr to sort it the way you want. You can even pipe the sorted result through something like awk -F'\t' '{print $2 "\t" $1}' to flip the columns back if you need them in that order

share|improve this answer

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.