I'm looking for the string foo=
in text files in a directory tree. It's on a common Linux machine, I have bash shell:
grep -ircl "foo=" *
In the directories are also many binary files which match "foo=". As these results are not relevant and slow down the search, I want grep to skip searching these files (mostly JPEG and PNG images). How would I do that?
I know there are the --exclude=PATTERN
and --include=PATTERN
options, but what is the pattern format? The man page of grep says:
--include=PATTERN Recurse in directories only searching file matching PATTERN.
--exclude=PATTERN Recurse in directories skip file matching PATTERN.
Searching on grep include, grep include exclude, grep exclude and variants did not find anything relevant
If there's a better way of grepping only in certain files, I'm all for it; moving the offending files is not an option. I can't search only certain directories (the directory structure is a big mess, with everything everywhere). Also, I can't install anything, so I have to do with common tools (like grep or the suggested find).
grep 2.5.3 introduced the --exclude-dir parameter which will work the way you want.
You can also set an environment variable: GREP_OPTIONS="--exclude-dir=.svn"
I'll second Andy's vote for ack though, it's the best.
find and xargs are your friends. Use them to filter the file list rather than grep's --exclude
Try something like
Look @ this one.
If you search non-recursively you can use glop patterns to match the filenames.
includes html and txt. It searches in the current directory only.
To search in the subdirectories:
In the subsubdirectories:
git grep
Use
git grep
which is optimized for performance and aims to search through certain files.By default it ignores binary files and it is honoring your
.gitignore
. If you're not working with Git structure, you can still use it by passing--no-index
.Example syntax:
For more examples, see:
Use the shell globbing syntax:
The syntax for
--exclude
is identical.Note that the star is escaped with a backslash to prevent it from being expanded by the shell (quoting it, such as
--include="*.{cpp,h}"
, would work just as well). Otherwise, if you had any files in the current working directory that matched the pattern, the command line would expand to something likegrep pattern -r --include=foo.cpp --include=bar.h rootdir
, which would only search files namedfoo.cpp
andbar.h
, which is quite likely not what you wanted.