How to automatically ignore files in grep

Is it possible to use grep to ignore some files when searching for something that is equivalent to svnignore or gitignore? I usually use something similar when searching for source code.

grep -r something * | grep -v ignore_file1 | grep -v ignore_file2 

Even if I could configure the grep alias to ignore these files, it would be nice.

+7
bash grep
source share
6 answers

- the option to exclude from grep will also work:

 grep perl * --exclude=try* --exclude=tk* 

It searches for perl in files in the current directory, excluding files starting with try or tk

+8
source share

You can also take a look at ack , which, among many other features, does not by default look for VCS directories such as .svn and .git.

+2
source share
 find . -path ./ignore -prune -o -exec grep -r something {} \; 

What this means is to find all the files in your current directory, excluding the directory (or file) named "ignore", and then run the grep -r command for each file found in unregistered files.

+1
source share

use shell extension

 shopt -s extglob for file in !(file1_ignore|file2_ignore) do grep ..... "$file" done 
+1
source share

I think grep does not have file filtering. To accomplish what you are trying to do, you can combine the find, xargs, and grep commands. My memory is not very good, so the example may not work:

 find -name "foo" | xargs grep "pattern" 

Find is flexible; you can use wildcards, ignore case, or use regular expressions. You can read the manuals for a full description.

after reading the next post, obviously grep has file name filtering.

0
source share

Here's a minimalist version of .gitignore. Requires standard utils: awk, sed (because my awk is so lame), egrep:

 cat > ~/bin/grepignore #or anywhere you like in your $PATH egrep -v "`awk '1' ORS=\| .grepignore | sed -e 's/|$//g' ; echo`" ^D chmod 755 ~/bin/grepignore cat >> ./.grepignore #above set to look in cwd ignorefile_1 ... ^D grep -r something * | grepignore 

grepignore creates a simple alternation clause:

 egrep -v ignorefile_one|ignorefile_two 

not incredibly effective, but good for manual use

0
source share

All Articles