What's the best way to find a string/regex match in files recursively? (UNIX)

前端 未结 8 1389

I have had to do this several times, usually when trying to find in what files a variable or a function is used.

I remember using xargs with grep in the past to do t

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-29 22:20

    The portable method* of doing this is

    find . -type f -print0 | xargs -0 grep pattern
    

    -print0 tells find to use ASCII nuls as the separator and -0 tells xargs the same thing. If you don't use them you will get errors on files and directories that contain spaces in their names.

    * as opposed to grep -r, grep -R, or grep --recursive which only work on some machines.

提交回复
热议问题