28

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 '14 at 14:13
32

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 \;
8
  • I must have a too old Linux... Oct 2 '14 at 14:41
  • I have edited the answer, try the find version if your grep doesn't support --exclude-dir.
    – jimmij
    Oct 2 '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... Oct 6 '14 at 9:21
  • 1
    Second solution doesn't use --exclude-dir, have you tried it?
    – jimmij
    Oct 6 '14 at 10:02
  • 3
    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

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
  • Code says e#, text says e% Feb 23 '17 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.