Thursday, April 2, 2009

Finding text in files from the Command-Line

This tip is posted everywhere, but for my love of copying and pasting, here's how to find any text in a directory of files quickly from the command line:

find . -name "*.php" -print | xargs grep "text_to_find"

To break this down:

find . -name "*.php" -print finds all files in the current or sub-directories that has the filename *.php and print out the file name

| This is the pipe. It sends the output of the above find command to the next command:

xargs takes the input from the find command and passes those files to the next command:

grep "text_to_find" searches in each of the files for the text "text_to_find"

No comments: