What is the best way to count “find” results?

前端 未结 4 1290
情歌与酒
情歌与酒 2020-11-27 04:07

My current solution would be find -exec printf \'.\' \\; | wc -c, but this takes far too long when there are more than 10000 results. Is there no f

4条回答
  •  無奈伤痛
    2020-11-27 04:31

    This is my countfiles function in my ~/.bashrc (it's reasonably fast, should work for Linux & FreeBSD find, and does not get fooled by file paths containing newline characters; the final wc just counts NUL bytes):

    countfiles () 
    { 
       command find "${1:-.}" -type f -name "${2:-*}" -print0 | 
           command tr -dc '\0' | command wc -c;
    return 0
    }
    
    countfiles
    
    countfiles ~ '*.txt'
    

提交回复
热议问题