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.

About this Archive

This page is an archive of entries from January 2011 listed from newest to oldest.

November 2010 is the previous archive.

February 2011 is the next archive.

Find recent content on the main index or look in the archives to find all content.

    follow me on Twitter