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

前端 未结 8 1364

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:25

    If you use the zsh shell you can use

    grep REGEX **/*
    

    or

    grep REGEX **/*.java
    

    This can run out of steam if there are too many matching files.

    The canonical way though is to use find with exec.

    find . -name '*.java' -exec grep REGEX {} \;
    

    or

    find . -type f -exec grep REGEX {} \;
    

    The 'type f' bit just means type of file and will match all files.

提交回复
热议问题