Alex Rivera | Logout

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

Asked 2008-10-09T05:31:27.063
34

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 this, but I am wondering if there are any easier ways.

Edit
Report

1 Answer

3

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.

answered 2008-10-09T05:47:15.487

Your Answer