Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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

Posted on 2014-03-09
Medium Priority
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 1000 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 35

Expert Comment

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

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 500 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 500 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 1000 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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.
Suggested Courses

721 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