Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have this structure in my linux filesystem:

/folder
...subfolder1
......temp/
...subfolder2
......temp/
...subfolder3
......temp/
...subfolder4
......temp/
...subfolder5
......temp/

And I want to get the total disk space that all the temp/ folders are using. I suppose it's something with df or du, but no idea on how to achieve it. I only need the sum of all the temp/ directories, not the other folders or files that may be in the subfolders. Any idea?

thanks!!

share|improve this question

migrated from stackoverflow.com Oct 17 '11 at 21:40

This question came from our site for professional and enthusiast programmers.

    
retagged bash so it doesn't get flagged for Serverfault (although you'll probably find a better answer there) –  Louis Oct 17 '11 at 21:02
    
The first line of the du manpage says 'Description: Summarize disk usage of each FILE, recursively for directories.' Using the du command without any flags should do the trick. –  Nick ODell Oct 17 '11 at 21:04

3 Answers 3

Try this:

#!/bin/bash
totalsize=0
for i in $(du -b | grep "/temp$" | cut -f1); do
        totalsize=$((totalsize + i))
done

totalsize=$(echo $totalsize | awk '{sum=$1; hum[1024**3]="Gb";hum[1024**2]="Mb"; hum[1024]="Kb"; for (x=1024**3; x>=1024; x/=1024){ if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x];break }}}')

echo "Total size: $totalsize"

It looks for every folder path that ends with /temp, and shows only the size in bytes. For each size found, add it to the total, and then before printing the results, use awk to convert bytes into human readable format.

share|improve this answer

I like Siavash's solution, it provides the total size on disk (in block increments).

Here's a byte count of files using find and awk:

find /folder -type f -wholename '*/temp/*' -printf '%s\n' | awk '{ total += $1 } END { print "Total:", total }'
share|improve this answer

You can use the below command to get the size of each temp directory and the grand total:

du -csh /folder/*/temp
share|improve this answer
    
this one worked perfectly!! thank you! –  shirkkan Oct 17 '11 at 21:35
    
@shirkkan: Please accept this answer if it is the best solution to your problem –  MattH Oct 18 '11 at 7:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.