Fast Linux file count for a large number of files

后端 未结 17 2667
名媛妹妹
名媛妹妹 2020-12-22 17:21

I\'m trying to figure out the best way to find the number of files in a particular directory when there are a very large number of files (more than 100,000).

When the

17条回答
  •  长情又很酷
    2020-12-22 17:58

    You can change the output based on your requirements, but here is a Bash one-liner I wrote to recursively count and report the number of files in a series of numerically named directories.

    dir=/tmp/count_these/ ; for i in $(ls -1 ${dir} | sort -n) ; { echo "$i => $(find ${dir}${i} -type f | wc -l),"; }
    

    This looks recursively for all files (not directories) in the given directory and returns the results in a hash-like format. Simple tweaks to the find command could make what kind of files you're looking to count more specific, etc.

    It results in something like this:

    1 => 38,
    65 => 95052,
    66 => 12823,
    67 => 10572,
    69 => 67275,
    70 => 8105,
    71 => 42052,
    72 => 1184,
    

提交回复
热议问题