find: Contents

1 
1 2.8 Contents
1 ============
1 
1 To search for files based on their contents, you can use the 'grep'
1 program.  For example, to find out which C source files in the current
1 directory contain the string 'thing', you can do:
1 
1      grep -l thing *.[ch]
1 
1    If you also want to search for the string in files in subdirectories,
1 you can combine 'grep' with 'find' and 'xargs', like this:
1 
1      find . -name '*.[ch]' | xargs grep -l thing
1 
1    The '-l' option causes 'grep' to print only the names of files that
1 contain the string, rather than the lines that contain it.  The string
1 argument ('thing') is actually a regular expression, so it can contain
1 metacharacters.  This method can be refined a little by using the '-r'
1 option to make 'xargs' not run 'grep' if 'find' produces no output, and
1 using the 'find' action '-print0' and the 'xargs' option '-0' to avoid
1 misinterpreting files whose names contain spaces:
1 
1      find . -name '*.[ch]' -print0 | xargs -r -0 grep -l thing
1 
1    For a fuller treatment of finding files whose contents match a
1 pattern, see the manual page for 'grep'.
1