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 find
and exec
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.
Why not this?
@PeterT is right. Almost all these answers invoke a command (du) for each file, which is very resource intensive and slow and unnecessary. The simplest and fastest way is this:
The previous answer is nice, but it has one problem: it sums the same disk usage several times because it takes into account the directory disk usage.
For example, I have a lot of files in my
~/tmp
directory:Running the first part of example posted by devnull to find the files modified in the last 24 hours, we can see that
awk
will sum the whole disk usage of the~/tmp
directory:But there is only one file modified in that period of time, with very little disk usage:
So we need to take into account only the files and exclude the directories:
You can also specify date ranges using the
-newermt
parameter. For example:See http://www.commandlinefu.com/commands/view/8721/find-files-in-a-date-range
du
wouldn't summarize if you pass a list of files to it.Instead, pipe the output to
cut
and letawk
sum it up. So you can say: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 (seetotal/1024
above).You can print file size with
find
using the-printf
option, but you still needawk
to sum.For example, total size of all files older than 365 days: