36

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
1
  • Not a direct answer, but you may want to try ack.
    – depquid
    Oct 1, 2014 at 14:13

3 Answers 3

38

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 \;
10
  • I must have a too old Linux... Oct 2, 2014 at 14:41
  • I have edited the answer, try the find version if your grep doesn't support --exclude-dir.
    – jimmij
    Oct 2, 2014 at 15:04
  • 1
    Second solution doesn't use --exclude-dir, have you tried it?
    – jimmij
    Oct 6, 2014 at 10:02
  • 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, 2018 at 10:11
  • 1
    This did not work for me on a Fedora. I used --exclude-dir='.[^.]*' instead.
    – russoue
    Nov 21, 2019 at 23:26
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.

1
  • 1
    Code says e#, text says e% Feb 23, 2017 at 14:12
0

On OSX, I'm using:

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .