How to Calculate the Total size of file searched using Find command

First retrieve the file size in bytes, then sum them up:

sed 's/\s\+/+/g' <<<$(find /storage/backup/rman/ -mtime +90 -mtime -120 -exec stat -c "%s" {} \;) | bc

EDIT

To see the files as well,

du -chs $(find /storage/backup/rman/ -mtime +90 -mtime -120)


Some versions of find (non-embedded Linux, Cygwin, OSX, FreeBSD) allow you to compare a file's modification time with a reference date with the -newermt operator.

find /storage/backup/rman -newermt '2012-12-01' ! -newermt '2013-01-01'

You can't use -mtime to tell whether a file was modified at a particular date, because this operator is relative to the time you run the find command. If your find doesn't have the -newermt operator, create reference files and use the -newer operator.

touch -t 201212010000 start
touch -t 201301010000 end
find /storage/backup/rman -newer start ! -newer end

To get the total size of the files, use du -c and keep only the last (“total”) line. You'll need to exclude directories, because when you pass a directory to du, it adds up the sizes of all the files under that directory.

find /storage/backup/rman -type f -newermt '2012-12-01' ! -newermt '2013-01-01' -exec du -c {} + | tail -n 1

If you have a large number of files, the length of the command line might be too large, so find will run du multiple times and the command above would only list the output from the last batch. In that case you'll need to add up the amounts from each run. Here's one way to do this.

find /storage/backup/rman -type f -newermt '2012-12-01' ! -newermt '2013-01-01' \
     -exec sh -c 'du "$@" | tail -n 1' _ {} + |
awk '{total += $1} END {print total}'

Exercise: what's wrong with the following command? (I.e. in what unusual but possible situation will it report a wrong figure?)

find /storage/backup/rman -type f -newermt '2012-12-01' ! -newermt '2013-01-01' \
     -exec du {} + |
awk '$2 == "total" {total += $1} END {print total}'

Tags:

Find

Rhel