Solved

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

Posted on 2014-03-09
8
598 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
ID: 39915826
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
ID: 39916599
Add -mindepth 1  to avoid getting the total sum (it stops find from finding  ".")
0
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 39916601
i.e. find /start/directory -type d -mindepth 1 | ...
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

Author Comment

by:TIMFOX123
ID: 39916607
great stuff,  I will check from work tomarrow !!
0
 
LVL 13

Assisted Solution

by:Sandy
Sandy earned 125 total points
ID: 39916796
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
ID: 39916838
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
ID: 39917229
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
ID: 39926532
all fantastic answers !!
you all are my "secret weapon" at work !!!
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Fetching data from Multiple Linux Server using Shell Scripting 55 138
Hacked File Timestamps 4 64
Linux VM 6 90
Powershell Timeout - Exchange 3 48
Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

831 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