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

Posted on 2014-03-09
Last Modified: 2014-03-13

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.


Question by:TIMFOX123
  • 2
  • 2
  • 2
  • +2
LVL 68

Assisted Solution

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"
LVL 34

Expert Comment

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

Expert Comment

by:Duncan Roe
ID: 39916601
i.e. find /start/directory -type d -mindepth 1 | ...

Author Comment

ID: 39916607
great stuff,  I will check from work tomarrow !!
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

LVL 13

Assisted Solution

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>


Assisted Solution

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

LVL 68

Accepted Solution

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"


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"

Author Closing Comment

ID: 39926532
all fantastic answers !!
you all are my "secret weapon" at work !!!

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

867 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

22 Experts available now in Live!

Get 1:1 Help Now