dig_123 dig_123 - 1 year ago 103
Linux Question

calculate total used disk space by files older than 180 days using find

I am trying to find the total disk space used by files older than 180 days in a particular directory. This is what I'm using:

find . -mtime +180 -exec du -sh {} \;

but the above is quiet evidently giving me disk space used by every file that is found. I want only the total added disk space used by the files. Can this be done using
command ?

Please note I simply don't want to use a script for this, it will be great if there could be a one liner for this. Any help is highly appreciated.

Answer Source

du wouldn't summarize if you pass a list of files to it.

Instead, pipe the output to cut and let awk sum it up. So you can say:

find . -mtime +180 -exec du -ks {} \; | cut -f1 | awk '{total=total+$1}END{print total/1024}'

Note that the option -h to display the result in human-readable format has been replaced by -k which is equivalent to block size of 1K. The result is presented in MB (see total/1024 above).