Google Answers Logo
View Question
 
Q: Listing files by size on unix base OSes ( Answered 5 out of 5 stars,   2 Comments )
Question  
Subject: Listing files by size on unix base OSes
Category: Computers
Asked by: lostandfound-ga
List Price: $4.00
Posted: 07 May 2004 10:05 PDT
Expires: 06 Jun 2004 10:05 PDT
Question ID: 342779
How can I output a list of all files in a directory or filesystem
sorted by size and printed in more human readable units such as MB
rather than bytes (a la 'ls -lh' or 'df -h')?

The answer I'm looking for will be:
* universal enough to work on Mac OS X, Linux, and Solaris
* simple enough that i won't have to have it aliased in my shell rc file
* as fast an efficient as possible
Answer  
Subject: Re: Listing files by size on unix base OSes
Answered By: passive-ga on 07 May 2004 10:37 PDT
Rated:5 out of 5 stars
 
Hi there,

From a bit of brief experimentation, it seems that the "ls" command
supports two options that would accomplish what you wanted.
"-S" Sorts descendingly by size.
"-h" Shows size in human readable format (but it looks like you
already knew that one).

So together, "ls -lhS" will do the job. I cannot confirm or deny that
this will work on Solaris or OS X, as I don't have them available to
test. But I do know that both Solaris and OS X use the "ls" command
for directory listing, so I have a strong suspicion that this will
work.

I don't consider this much information to give up, so if there is
anything else I can help you find out, let me know.

Clarification of Answer by passive-ga on 07 May 2004 11:36 PDT
topbanana is correct, Solaris 8 accepts neither "-h" nor "-S".
Solaris 9 seems to accept "-h".
The shortest thing I found that will do sorting by size under solaris is: 

ls -al | sort -n -r --key=4,5 | more

Found at:
http://www.cgd.ucar.edu/gds/thibaud/UNIX-tips/Tips/ls-ps.html
It's hardly concise, and it's still not human readable.

Under Solaris 9, you could add "-h" in to that, but sort doesn't
recognize human readable, so the sorting would not be particularily
good.

ls -ahl | sort -n -r --key=4,5 | more

Request for Answer Clarification by lostandfound-ga on 07 May 2004 12:22 PDT
My apology... I meant to include that it should recursively list all
files in the subdirectories of the partition or directory that I
specify.

Clarification of Answer by passive-ga on 07 May 2004 12:46 PDT
Well, there is another flag that provides that:
"-R" Recursively lists subdirectories.

This works by displaying the current directory, then each
subdirectory. This means the sorting is broken up by directory, so a
large file in a subdirectory will be displayed below a small file in a
parent directory.
This should work on Solaris as well.
Will this accomplish what you want, or do you want a sorted list that
includes everything in a single list?

ls -ShlR

Request for Answer Clarification by lostandfound-ga on 07 May 2004 13:29 PDT
I really need a single sorted list.  If a filesystem is nearly full, I
need a quick means of determining the largest files.  Seperate lists
for each directory would be unmanagable if there are a lot of
directories.

Clarification of Answer by passive-ga on 07 May 2004 18:11 PDT
That's what I figured. 
The following, though not perfectly concise, should do what you need.

du -a | sort -n -r | more

use "q" to quit "more"

It doesn't accomplish the human readability part, solely because
"sort" doesn't understand it. As it is, it's fairly easy to tell who
the largest files are.
I could probably figure out a way to get it human readable, but it
would be much longer than this, and not easy to remember.
lostandfound-ga rated this answer:5 out of 5 stars and gave an additional tip of: $1.00
Thanks for your help!  This seems to do the trick.

Comments  
Subject: Re: Listing files by size on unix base OSes
From: topbanana-ga on 07 May 2004 11:12 PDT
 
Might just be me but the -h switch isn't available on Solaris, unless
you know otherwise?
Subject: Re: Listing files by size on unix base OSes
From: lostandfound-ga on 07 May 2004 12:21 PDT
 
My apology... I meant to include that it should recursively list all
files in the subdirectories of the partition or directory that I
specify.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy