Disk usage of files whose names match a regex, in Linux?

前端 未结 6 1611
耶瑟儿~
耶瑟儿~ 2020-12-23 21:19

So, in many situations I wanted a way to know how much of my disk space is used by what, so I know what to get rid of, convert to another format, store elsewhere (such as da

6条回答
  •  一整个雨季
    2020-12-23 21:57

    If you're OK with glob-patterns and you're only interested in the current directory:

    stat -c "%s" *.bak | awk '{sum += $1} END {print sum}'
    

    or

    sum=0
    while read size; do (( sum += size )); done < <(stat -c "%s" *.bak)
    echo $sum
    

    The %s directive to stat gives bytes not kilobytes.

    If you want to descend into subdirectories, with bash version 4, you can shopt -s globstar and use the pattern **/*.bak

提交回复
热议问题