10

I am searching a dir for a particular string (to see all instances of where the string is present and in which files). However, I want to exclude one particular file from being searched.

Here's what is happening-

$echo "searchstring" > ./old_folder/useless_file $echo "searchstring" > ./new_folder/good_file $grep -r --exclude="old_folder/useless_file" searchstring * ./old_folder/useless_file:searchstring ./new_folder/good_file:searchstring 

Here is the output I want-

./new_folder/good_file:searchstring 
1
  • 7
    If you change --exclude="old_folder/useless_file" to simply --exclude=useless_file do you get the same problem? (My reading of the man page suggests the pattern to --exclude should be the basename only, not including the path.) Commented Mar 30, 2016 at 15:51

1 Answer 1

12

The --exclude option takes globs that are matched against file names, not directories or full paths:

 --exclude=GLOB Skip files whose base name matches GLOB (using wildcard matching). A file-name glob can use *, ?, and [...] as wildcards, and \ to quote a wildcard or backslash character literally. 

So, you could do:

$ grep -r --exclude="*useless_file*" searchstring new_folder/good_file:searchstring 

Or, to exclude all files in that directory:

$ grep -r --exclude-dir="old_folder" searchstring new_folder/good_file:searchstring 

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.