Solved

little bash script to list all directories (& subdirectories ) and sum up the disk space of each

Posted on 2014-03-09
8
585 Views
Last Modified: 2014-03-13
Hello

I am looking for little bash script to list all directories (& subdirectories ) and sum up the disk space of each  directory (& subdirectory ) .

Yes I want bash, not perl :)

It would be nice to do everything in k ( kilobytes )  so I know longer numbers means more space.

It would also be nice to show in order the largest directories or largest files in order

Oh, redhat 5&6 is what it needs to run on :).  Also I have no control of anything so installing any software is not an option.

thx

timfox
0
Comment
Question by:TIMFOX123
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 250 total points
Comment Utility
find /start/directory -type d | xargs du -kSs |sort -n

Please note the "-S" (uppercase "S") flag of "du"!

That's the "separate-dirs" option to report the size of each directory separately, not including the sizes of subdirectories.
"-k" means: report in kilobytes, "-s" means: summarize.

Add the "-c" option to have "du" produce a "total" at the end of the report:

find /start/directory -type d | xargs du -kSsc |sort -n

To revert the sort order (i.e. largest directory first) use the "-r" flag of "sort" ("sort -rn").
Attention: the total sum will consequently appear at the top!

Finally, to exclude zero-size directories use "| grep -v "^0"" at the end of the command chain:

find /start/directory -type d | xargs du -kSsc |sort -rn | grep -v "^0"
0
 
LVL 34

Expert Comment

by:Duncan Roe
Comment Utility
Add -mindepth 1  to avoid getting the total sum (it stops find from finding  ".")
0
 
LVL 34

Expert Comment

by:Duncan Roe
Comment Utility
i.e. find /start/directory -type d -mindepth 1 | ...
0
 

Author Comment

by:TIMFOX123
Comment Utility
great stuff,  I will check from work tomarrow !!
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 13

Assisted Solution

by:Sandy
Sandy earned 125 total points
Comment Utility
or else.. if you just wanted to see which all files are the biggest contributor... you can use below command

#find <fs> -xdev -ls | sort -nr -k 7 | head <numberoflines>

TY/SA
0
 
LVL 2

Assisted Solution

by:gdarcan
gdarcan earned 125 total points
Comment Utility
for f in `find . -type d`;do du -s $f;done;

Open in new window

or human readable version
for f in `find . -type d`;do du -s -h $f;done;

Open in new window

0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 250 total points
Comment Utility
I forgot a maybe important thing:

If you have (or suspect to have) directory names containing embedded spaces we must make some changes:

find /start/directory -type d -print0 | xargs -0 du -kSs |sort -n

find /start/directory -type d -print0 | xargs -0 du -kSsc |sort -n

find /start/directory -type d -print0 | xargs -0 du -kSsc |sort -rn | grep -v "^0"

OR

find /start/directory -type d -print0 | du -kSs --files0-from=-|sort -n

find /start/directory -type d -print0 | du -kSsc --files0-from=- |sort -n

find /start/directory -type d -print0 | du -kSsc --files0-from=- |sort -rn | grep -v "^0"
0
 

Author Closing Comment

by:TIMFOX123
Comment Utility
all fantastic answers !!
you all are my "secret weapon" at work !!!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now