January 2011 Archives
This useful command will find all files where a given string, word, value etc is contained within the file. It will search recursively through all directories from where you are presently within the file system:
find RANGE -iname ‘RULES’ | xargs grep ‘STRING’ -GREP_ARGUMENTS
- RANGE = where you are searching. Using a . (full stop) will search all from where you presently are
- RULES = elements of a filename. For instance, ‘*.doc’ will only look through files ending in .doc - * is used as a wildcard
- STRING = the needle in the haystack that we are looking for
Typical Grep Arguments Used
- -s is the same as —no-messages. From man grep: “Suppress error messages about nonexistent or unreadable files.”
- -l, —files-with-matches. From man grep: “Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning will stop on the first match.” Basically this just outputs the filename.
- See man grep for more
The Final Result Example (I tend to use this a lot):
find . -iname ‘*php’ | xargs grep ‘needle’ -sl
This looks for the value ‘needle’ in php files only and will only display the file names.