35

I have a svn local copy, i want to make a search on the string some_string.

$ grep some_string * -r
lang/en:some_string=Some string
lang/.svn/en:some_string=Some string

But if a go little further, it seems that first level hidden directories are excluded :

$ cd lang && grep some_string * -r
en:some_string=Some string

How can I remove the hidden svn diretories from my output, and not only from the first depth level?

$ grep some_string * -r --which_option_here?
lang/en:some_string=Some string
MUY Belgium
  • 1,255

3 Answers3

37

Use --exclude-dir option, e.g.:

grep -r --exclude-dir='.*' some_string

From man grep:

--exclude-dir=DIR
              Exclude directories matching the pattern DIR from recursive searches.

Note however, that --exclude-dir option is available only in GNU grep. If your grep doesn't support it, you may need to use other tool as find for example:

find . \( -name .svn -prune \) -o -name "*" -exec grep -H "some_string" {} 2>/dev/null \;
jimmij
  • 47,140
  • I must have a too old Linux... – MUY Belgium Oct 02 '14 at 14:41
  • I have edited the answer, try the find version if your grep doesn't support --exclude-dir. – jimmij Oct 02 '14 at 15:04
  • I have version grep version 2.5.1, and there is no --exclude-dir argument, and I have tried everything I thought of... – MUY Belgium Oct 06 '14 at 09:21
  • 1
    Second solution doesn't use --exclude-dir, have you tried it? – jimmij Oct 06 '14 at 10:02
  • I have "unreconized option" – MUY Belgium Oct 06 '14 at 13:12
  • That's because the -H option is also available only with gnu grep. Try it with grep "pattern" /dev/null {}... instead of grep -H "pattern" {}... – don_crissti Aug 23 '15 at 01:14
  • 7
    side note: grep -r --exclude-dir='.*' . will exclude everything, because everything starts with ./ when you specify . as an explicit path argument. So be careful not to specify the current directory explicitly, or use an exclusion pattern like --eclude-dir='.?*', which matches and excludes only dirs with at least one character after the dot. – Robin479 Nov 23 '18 at 10:11
  • 1
    This did not work for me on a Fedora. I used --exclude-dir='.[^.]*' instead. – russoue Nov 21 '19 at 23:26
  • This absolutely fails when using on the current directory . (big use case). It will ALWAYS return NOTHING. – WalksB Jul 08 '22 at 23:46
  • I just learned that if you don't pass any argument for the directory, grep still works on the current directory (but doesn't face the exclude current directory problem). So I guess with this workaround it's not a big use case. – WalksB Jul 09 '22 at 00:19
1

I normally use this:

for e in $(find . -maxdepth 1 -type d); do echo ${e#\./}; done | grep -v '^\.'

${e%\./} will remove the first ./ and then grep removes everything that begins with a dot.

Manuel
  • 111
0

On OSX, I'm using:

grep -lR --exclude-dir={'*node_modules*',build,'./.*'} 'SearchTerm'