total size of group of files selected with 'find'

ShellScriptingFindGrepFilesize

Shell Problem Overview


For instance, I have a large filesystem that is filling up faster than I expected. So I look for what's being added:

find /rapidly_shrinking_drive/ -type f -mtime -1 -ls | less

And I find, well, lots of stuff. Thousands of files of six-seven types. I can single out a type and count them:

find /rapidly_shrinking_drive/ -name "*offender1*" -mtime -1 -ls | wc -l

but what I'd really like is to be able to get the total size on disk of these files:

find /rapidly_shrinking_drive/ -name "*offender1*" -mtime -1 | howmuchspace

I'm open to a Perl one-liner for this, if someone's got one, but I'm not going to use any solution that involves a multi-line script, or File::Find.

Shell Solutions


Solution 1 - Shell

The command du tells you about disk usage. Example usage for your specific case:

find rapidly_shrinking_drive/ -name "offender1" -mtime -1 -print0 | du --files0-from=- -hc | tail -n1

(Previously I wrote du -hs, but on my machine that appears to disregard find's input and instead summarises the size of the cwd.)

Solution 2 - Shell

Darn, Stephan202 is right. I didn't think about du -s (summarize), so instead I used awk:

find rapidly_shrinking_drive/ -name "offender1" -mtime -1 | du | awk '{total+=$1} END{print total}'

I like the other answer better though, and it's almost certainly more efficient.

Solution 3 - Shell

with GNU find,

 find /path -name "offender" -printf "%s\n" | awk '{t+=$1}END{print t}'

Solution 4 - Shell

I'd like to promote jason's comment above to the status of answer, because I believe it's the most mnemonic (though not the most generic, if you really gotta have the file list specified by find):

$ du -hs *.nc
6.1M  foo.nc
280K  foo_region_N2O.nc
8.0K  foo_region_PS.nc
844K  foo_region_xyz.nc
844K  foo_region_z.nc
37M   ETOPO1_Ice_g_gmt4.grd_region_zS.nc
$ du -ch *.nc | tail -n 1
45M total
$ du -cb *.nc | tail -n 1
47033368  total

Solution 5 - Shell

Recently i faced the same(almost) problem and i came up with this solution.

find $path -type f -printf '%s '

It'll show files sizes in bytes, from man find:

-printf format
    True; print format on the standard output, interpreting `\' escapes and `%' directives.  Field widths and precisions can be spec‐
    ified as with the `printf' C function.  Please note that many of the fields are printed as %s rather than %d, and this  may  mean
    that  flags  don't  work as you might expect.  This also means that the `-' flag does work (it forces fields to be left-aligned).
    Unlike -print, -printf does not add a newline at the end of the string.
    ...
    %s  File's size in bytes.
    ...

And to get a total i used this:

echo $[ $(find $path -type f -printf %s+)0] #b
echo $[($(find $path -type f -printf %s+)0)/1024] #Kb
echo $[($(find $path -type f -printf %s+)0)/1024/1024] #Mb
echo $[($(find $path -type f -printf %s+)0)/1024/1024/1024] #Gb

Solution 6 - Shell

I have tried all this commands but no luck. So I have found this one that gives me an answer:

find . -type f -mtime -30 -exec ls -l {} \; | awk '{ s+=$5 } END { print s }'

Solution 7 - Shell

You could also use ls -l to find their size, then awk to extract the size:

find /rapidly_shrinking_drive/ -name "offender1" -mtime -1 | ls -l | awk '{print $5}' | sum

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEd HyerView Question on Stackoverflow
Solution 1 - ShellStephan202View Answer on Stackoverflow
Solution 2 - Shellpix0rView Answer on Stackoverflow
Solution 3 - Shellghostdog74View Answer on Stackoverflow
Solution 4 - ShellTomRocheView Answer on Stackoverflow
Solution 5 - ShellIvanView Answer on Stackoverflow
Solution 6 - ShellVlad SavitskyView Answer on Stackoverflow
Solution 7 - ShellSinan TaifourView Answer on Stackoverflow