LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   can you specify which files to grep search? (https://www.linuxquestions.org/questions/linux-software-2/can-you-specify-which-files-to-grep-search-371760/)

sneakyimp 10-10-2005 11:30 PM

can you specify which files to grep search?
 
i have a complex directory with many files and many subdirectories. I would like to grep for patterns in some php files, html files, javascript files, while ignoring .gz files, image files, and files in certain directories.

is there a way to do this? i have tried the --include and --exclude flags for grep but they don't appear a) to support multiple file extensions in a single expression b) to allow you to exclude entire subdirectories.

I'm guessing that there might be some way to create a list of the files i want searched and perhaps pipe that to the grep command? that would be awesome. if someone could help me, i would appreciate it.

slackhack 10-11-2005 12:02 AM

try:

grep -i '<string>' | ./*.php *.html *.js

that would search only php, html, and js files while ignoring anything else in the directory.

sneakyimp 10-11-2005 02:17 AM

thanks for the suggestion...i need it to recurse subdirs (excepting ones i want to specifically exclude).

the ideal situation would be to generate a file list and only check those files..

berbae 10-11-2005 08:11 AM

I think this can do what you ask :

find . -type f \( -name \*.php -o -name \*.html -o -name \*.js \) | grep -v -e <dir1_to_exclude> -e <dir2_to_exclude> | xargs grep --with-filename <string_to_search>

Hope this helps.

sneakyimp 10-12-2005 08:28 PM

wow!

pretty gnarly. i will give that a try.
THANKS!


All times are GMT -5. The time now is 08:46 PM.