Fast Linux file count for a large number of files

后端 未结 17 2613
名媛妹妹
名媛妹妹 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:59

    Surprisingly for me, a bare-bones find is very much comparable to ls -f

    > time ls -f my_dir | wc -l
    17626
    
    real    0m0.015s
    user    0m0.011s
    sys     0m0.009s
    

    versus

    > time find my_dir -maxdepth 1 | wc -l
    17625
    
    real    0m0.014s
    user    0m0.008s
    sys     0m0.010s
    

    Of course, the values on the third decimal place shift around a bit every time you execute any of these, so they're basically identical. Notice however that find returns one extra unit, because it counts the actual directory itself (and, as mentioned before, ls -f returns two extra units, since it also counts . and ..).

提交回复
热议问题