Go to the first, previous, next, last section, table of contents.


Contents

To search for files based on their contents, you can use the grep program. For example, to find out which C source files in the current directory contain the string `thing', you can do:

grep -l thing *.[ch]

If you also want to search for the string in files in subdirectories, you can combine grep with find and xargs, like this:

find . -name '*.[ch]' | xargs grep -l thing

The `-l' option causes grep to print only the names of files that contain the string, rather than the lines that contain it. The string argument (`thing') is actually a regular expression, so it can contain metacharacters. This method can be refined a little by using the `-r' option to make xargs not run grep if find produces no output, and using the find action `-print0' and the xargs option `-0' to avoid misinterpreting files whose names contain spaces:

find . -name '*.[ch]' -print0 | xargs -r -0 grep -l thing

For a fuller treatment of finding files whose contents match a pattern, see the manual page for grep.


Go to the first, previous, next, last section, table of contents.