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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 | ...
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  


Author Comment

ID: 39916607
great stuff,  I will check from work tomarrow !!
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Recently, an awarded photographer, Selina De Maeyer (, completed a photo shoot of a beautiful event ( in An…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

751 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