find -exec cmd {} + vs | xargs

前端 未结 3 454
半阙折子戏
半阙折子戏 2020-12-22 14:48

Which one is more efficient over a very large set of files and should be used?

find . -exec cmd {} +

or

find . | xargs cmd
         


        
3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-22 15:15

    find . | xargs cmd
    

    is more efficient (it runs cmd as few times as possible, unlike exec, which runs cmd once for each match). However, you will run into trouble if filenames contain spaces or funky characters.

    The following is suggested to be used:

    find . -print0 | xargs -0 cmd
    

    this will work even if filenames contain funky characters (-print0 makes find print NUL-terminated matches, -0 makes xargs expect this format.)

提交回复
热议问题