recursive grep: exclude specific directories

You might look into ack.

I've just started using it but it seems well-suited for this.


grep -r --exclude-dir=dev --exclude-dir=sys --exclude-dir=proc PATTERN data

Source: https://stackoverflow.com/questions/2799246/grep-excluding-a-specific-folder-using


you can use find instead:

find . -not -path "*/.svn*" -not -type d -exec grep -ni "myfunc" {} \; -print

OK, so that's a little backwards, you get the grep results first and then the path. Maybe someoe else has a better answer?


Here's a full example from a script in one of my projects that might help, I call this file "all_source" (marked as executable) and put it in my project's root dir then call it like grep myfunc $(./all_source) the sort at the end of the script is totally optional.

#!/bin/bash

find . \
    -type d \( \
            -wholename './lib' -o \
            -wholename './vc6' -o \
            -name 'gen' -o \
            -name '.svn' \
            \) -prune -o \
    -type f \( \
            -name '*.h' -o \
            -name '*.cpp' -o \
            -name '*.c' -o \
            -name '*.lua' -o \
            -name '*.*awk' \) -print \
    | sort

This script returns all the file names in the project that match *.h, *.cpp, *.c, *.lua, *.*awk, but doesn't search in all folders named .svn and gen folders as well as skipping the folders for ./lib and ./vc6 (but only the ones right off the project root). So when you do grep myfunc $(./all_source) it only greps in those files. You'll need to call this from the root dir of the project as well.