total size of group of files selected with 'find'

后端 未结 7 1910
庸人自扰
庸人自扰 2020-12-07 16:32

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          


        
相关标签:
7条回答
  • 2020-12-07 17:06

    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
    
    0 讨论(0)
  • 2020-12-07 17:15

    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.

    0 讨论(0)
  • 2020-12-07 17:23

    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 }'
    
    0 讨论(0)
  • 2020-12-07 17:23

    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
    
    0 讨论(0)
  • 2020-12-07 17:27

    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.)

    0 讨论(0)
  • 2020-12-07 17:29

    with GNU find,

     find /path -name "offender" -printf "%s\n" | awk '{t+=$1}END{print t}'
    
    0 讨论(0)
提交回复
热议问题