How do I find a phrase/word recursively in a file tree in Linux?

grep -r "register_long_arrays" *

will recursively find all occurrences of register_long_arrays in your current directory.

If you want to combine find with grep to limit the types of files searched you should use it like this (this example will limit the search to files ending .txt):

find . -name '*.txt' -exec grep "register_long_arrays" {} \;

The {} is replaced with every file that find finds without you having to worry about escaping problem characters like spaces. Note the backslash before the semicolon. This ensures that the semicolon is escaped and passed to the exec clause (grep) rather than terminating the find command.

If you're still not finding what you want it may be a case issue, (supply the -i flag to grep to ignore case) or perhaps the content you want is in a hidden file (starting with .), which * will not match, in which case supply both * and .* as arguments to grep.


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

will work to search all regular files, but it invokes grep once for every file. This is inefficient and will make the search take noticeably longer on slow computers and/or large directory hierarchies. For that reason, it is better to use xargs:

find . -type f | xargs grep my_phrase

or, if you have files whose names contain spaces,

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

Even simpler is to just use the -r option to grep:

grep -r my_phrase .

Note the use of . rather than * as the file name argument. That avoids the problem of the expansion of * not including hidden files.