5

I need to use:

grep -rio pattern --exclude-dir=".config/dir1*" --exclude-dir=".test*"

in order to grep for that pattern avoiding all the folders that contain .config/dir1 and .test in the name,

E.g. .config/dir1, .config/dir1/dir2, .test and ./other/.test should be excluded.

What I did does not exclude those folders, could you please help me?

Thanks

1 Answer 1

8

Your command does exclude files in .test/ and in other/.test/.

It doesn't exclude files in .config/dir1 because --exclude-dir only looks at the base name of the directory, not at its full path. You would need --exclude-dir="dir1", which excludes elsewhere/dir1 as well. Note that if a directory is excluded, its subdirectories are excluded as well, so --exclude-dir="dir1" also excludes files in .config/dir1/dir2.

If grep's exclude patterns aren't sufficient for you, you can combine find with grep. The syntax of find is more complex, but it's more powerful. For example, you can exclude .config/dir1 while not excluding other directories called dir1. (I assume that you have GNU find since you have GNU grep.)

find . \
     -path "./.config/dir1" -prune -o \
     -name ".test" -prune -o \
     -type f -print0 |
  xargs -0 grep -io pattern
1
  • Hi, any idea how to do the same in GrepWin for Win. 10? Let's say exclude any dir that has the word "not". The help says for exact matches I should use ^(not)$, but what about if not is part of the name, like nottingham. Commented Apr 24, 2023 at 22:46

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.