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

前端 未结 6 1622
耶瑟儿~
耶瑟儿~ 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 22:04

    Run this in a Bourne Shell to declare a function that calculates the sum of sizes of all the files matching a regex pattern in the current directory:

    sizeofregex() { IFS=$'\n'; for x in $(find . -regex "$1" 2> /dev/null); do du -sk "$x" | cut -f1; done | awk '{s+=$1} END {print s}' | sed 's/^$/0/'; unset IFS; }
    

    (Alternatively, you can put it in a script.)

    Usage:

    cd /where/to/look
    sizeofregex 'myregex'
    

    The result will be a number (in KiB), including 0 (if there are no files that match your regex).

    If you do not want it to look in other filesystems (say you want to look for all .so files under /, which is a mount of /dev/sda1, but not under /home, which is a mount of /dev/sdb1, add a -xdev parameter to find in the function above.

提交回复
热议问题