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

前端 未结 4 1307
情歌与酒
情歌与酒 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:36

    This solution is certainly slower than some of the other find -> wc solutions here, but if you were inclined to do something else with the file names in addition to counting them, you could read from the find output.

    n=0
    while read -r -d ''; do
        ((n++)) # count
        # maybe perform another act on file
    done < <(find  -print0)
    echo $n
    

    It is just a modification of a solution found in BashGuide that properly handles files with nonstandard names by making the find output delimiter a NUL byte using print0, and reading from it using '' (NUL byte) as the loop delimiter.

提交回复
热议问题