I want to tell grep, on Debian, not to search through files in /proc or /sys. But if I use:





--exclude-dir=/proc --exclude-dir=/sys

Then grep still read /sys and crashes because of that. So how can I tell grep to skip the /proc and /sys directories?

  • First, what exactly are you trying to do? Are you searching for a pattern recursively? – Luciano Andress Martini Jan 10 '19 at 14:23
  • I already answered my question. Moderator has just deleted it – wxi Jan 10 '19 at 18:08
  • 1
    Sorry about that. Since there was no actual question here, I thought you were wondering why these fail, not how to do it. And someone else must have thought so as well since they had edited your answer into your question. – terdon Jan 10 '19 at 21:48
  • It isn't a possible duplicate – wxi Jan 11 '19 at 9:36
  • @don_crissti it's not a duplicate of that. Is there a q&a about how grep's --exclude-dir is matching directory names (eg. sys), not paths (eg. /sys)? – mosvy Jan 11 '19 at 23:42

The documentatiton for --exclude-dir in the GNU grep manual says


Skip any command-line directory with a name suffix that matches the pattern GLOB. When searching recursively, skip any subdirectory whose base name matches GLOB. Ignore any redundant trailing slashes in GLOB.

As you can see, the given pattern (GLOB) will be applied only to the actual filename of the directory, and since a directory name can't contain / in its name, a pattern like /proc will never match.

Therefore, you would have to use --exclude-dir=proc and --exclude-dir=sys (or --exclude-dir={proc,sys} if you are short on time), and at the same time be aware that this would skip not only /proc and /sys but also any other directory with either of those names.

Another way of recursively searching a complete directory tree from the root down while avoiding these two directories is by using grep from find:

find / \( -type d \( -path /proc -o -path /sys \) -prune \) -o \
    -type f -exec grep 'PATTERN' {} +

This would detect the two specific directories /proc and /sys and stop find from descending into them. It would also feed any found regular file to grep in as large batches as possible at a time.

| improve this answer | |
  • why do you use -prune? – Shuzheng Nov 4 at 9:29
  • @Shuzheng To stop find from entering those directories. – Kusalananda Nov 4 at 10:41
  • ♦ - Will you elaborate? What directories specifically? – Shuzheng Nov 6 at 7:40
  • @Shuzheng /proc and /sys. These are the directories that the user in the question wants to avoid. – Kusalananda Nov 6 at 7:41
  • Couldn't this be written using negation instead? ! \( -path /proc -o -path /sys \)? – Shuzheng Nov 6 at 13:20

You must know that grep will take the selected directory as the base for the file names on the search, because that on the search list they will be showed as dev/, sys/ etc. so instead of using:

grep 'yourstring' -R --exclude-dir={/dev,/sys,/proc} / 

You must use:

grep 'yourstring '-R --exclude-dir={dev/,sys/,proc/} /
| improve this answer | |
  • That's what i said but a moderator has deleted my answer. So we can't answer ourselves here. "But --exclude-dir={proc,sys} works well !" – wxi Jan 10 '19 at 18:06

However, this works well!

| improve this answer | |
  • 2
    Sorry! I misunderstood completely and thought your question was "why does --exclude-dir=/proc and --exclude-dir=/proc --exclude-dir=/sys not work. So an answer would explain why those fail, not give a command that works. If you are instead asking "how can I do this", then this is indeed an answer and I was wrong to delete it. If this is the case, could you please also edit your question so what you are asking is clearer? – terdon Jan 10 '19 at 21:47
  • Someone has already done it, but thanks for your kindness ! – wxi Jan 11 '19 at 9:37
  • Hmmm.... I would be interested in knowing what the difference between proc and /proc would be and why using /proc does not exclude the /proc directory. It seems to me that using proc would make grep also exclude directories with this name in other parts of the directory structure. – Kusalananda Jan 11 '19 at 9:47
  • I read in first time in a other place that it take /proc but ./proc too. And i read here that because i have precised "/" in the location of search, grep does need to read proc and not /proc, because it's relative to where he begin to search – wxi Jan 11 '19 at 13:47
  • @Kusalananda maybe because --exclude-dir's pattern should match the name, not the path of the directory? – mosvy Jan 11 '19 at 23:40

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.