3

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!!

2
  • retagged bash so it doesn't get flagged for Serverfault (although you'll probably find a better answer there)
    – Louis
    Oct 17, 2011 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, 2011 at 21:04

3 Answers 3

6

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

du -csh /folder/*/temp
1
  • @shirkkan: Please accept this answer if it is the best solution to your problem
    – MattH
    Oct 18, 2011 at 7:29
0

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 }'
0

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.

You must log in to answer this question.