Rectangle 27 730

Recent versions of GNU Grep (>= 2.5.2) provide:

--exclude-dir=dir

which excludes directories matching the pattern dir from recursive directory searches.

grep -R --exclude-dir=node_modules 'some pattern' /path/to/search

For older GNU Greps and POSIX Grep, use find as suggested in other answers.

Or just use ack (Edit: or The Silver Searcher) and be done with it!

@Manocho: If you think ack is great, try The Silver Searcher and see the speed increase!

Syntax for the impatient: --exclude-dir=dir uses grep's regular expression patterns, not shell's file globbing. Patterns work on paths relative to your current directory. So use pattern --exclude-dir=dir, not --exclude-dir="/root/dir/*".

$ grep -r --exclude-dir=dir1 --exclude-dir=dir2 "string" /path/to/search/dir

@Johnsyweb, I was hoping for something where I did not have to mention --exclude-dir multiple times.

grep -r "Request" . --exclude-dir={node_modules,git,build}

linux - How can I exclude directories from grep -R? - Stack Overflow

linux unix grep
Rectangle 27 730

Recent versions of GNU Grep (>= 2.5.2) provide:

--exclude-dir=dir

which excludes directories matching the pattern dir from recursive directory searches.

grep -R --exclude-dir=node_modules 'some pattern' /path/to/search

For older GNU Greps and POSIX Grep, use find as suggested in other answers.

Or just use ack (Edit: or The Silver Searcher) and be done with it!

@Manocho: If you think ack is great, try The Silver Searcher and see the speed increase!

Syntax for the impatient: --exclude-dir=dir uses grep's regular expression patterns, not shell's file globbing. Patterns work on paths relative to your current directory. So use pattern --exclude-dir=dir, not --exclude-dir="/root/dir/*".

$ grep -r --exclude-dir=dir1 --exclude-dir=dir2 "string" /path/to/search/dir

@Johnsyweb, I was hoping for something where I did not have to mention --exclude-dir multiple times.

grep -r "Request" . --exclude-dir={node_modules,git,build}

linux - How can I exclude directories from grep -R? - Stack Overflow

linux unix grep
Rectangle 27 93

grep -nr string my_directory

Additional notes: this satisfies the syntax grep [options] string filename because in Unix-like systems, a directory is a kind of file (there is a term "regular file" to specifically refer to entities that are called just "files" in Windows).

grep -nr string reads the content to search from the standard input, that is why it just waits there for input from you, and stops doing so when you press ^C (it would stop on ^D as well, which is the key combination for end-of-file).

Hey, so if i want to search for a string irrespective of the case, must I do this: grep -i -nr "my word" .

grep -inr "my word" .

@kiki: -r for grep means search in subdirectories recursively and -n means prefix each line of output with the corresponding line number of the file which contains that line. man grep describes all of this, and much more.

How can I use grep to find a word inside a folder? - Stack Overflow

grep
Rectangle 27 91

grep -nr string my_directory

Additional notes: this satisfies the syntax grep [options] string filename because in Unix-like systems, a directory is a kind of file (there is a term "regular file" to specifically refer to entities that are called just "files" in Windows).

grep -nr string reads the content to search from the standard input, that is why it just waits there for input from you, and stops doing so when you press ^C (it would stop on ^D as well, which is the key combination for end-of-file).

Hey, so if i want to search for a string irrespective of the case, must I do this: grep -i -nr "my word" .

grep -inr "my word" .

@kiki: -r for grep means search in subdirectories recursively and -n means prefix each line of output with the corresponding line number of the file which contains that line. man grep describes all of this, and much more.

How can I use grep to find a word inside a folder? - Stack Overflow

grep
Rectangle 27 6

Do a man grep on your system, and see what version you have. Your version of grep may not be able to use --exclude-dirs.

You're really better off using find to find the files you want, then use grep to parse them:

$ find . -name '.git' -type d -prune \
     -o -name "*.min.*" -prune \
     -o -type f -exec grep --color -n -H {} "$pattern" \;

I'm not a fan of the recursive grep. Its syntax has become bloated, and it's really unnecessary. We have a perfectly good tool for finding files that match a particular criteria, thank you.

In the find program, the -o separate out the various clauses. If a file has not been filtered out by a previous -prune clause, it is passed to the next one. Once you've pruned out all of the .git directories and all of the *.min.* files, you pass the results to the -exec clause that executes your grep command on that one file.

$ find . -name '.git' -type d -prune \
     -o -name "*.min.*" -prune \
     -o -type f -print0 | xargs -0 grep --color -n -H "$pattern"

The -print0 prints out all of the found files separated by the NULL character. The xargs -0 will read in that list of files and pass them to the grep command. The -0 tells xargs that the file names are NULL separated and not whitespace separated. Some xargs will take --null instead of the -0 parameter.

bash function grep --exclude-dir not working - Stack Overflow

bash grep find
Rectangle 27 4

to find recursively you can use flags -R (recursive) and --include (GLOB pattern). See:

linux - How can I search for a multiline pattern in a file? - Stack Ov...

linux command-line grep find pcregrep
Rectangle 27 4

to find recursively you can use flags -R (recursive) and --include (GLOB pattern). See:

linux - How can I search for a multiline pattern in a file? - Stack Ov...

linux command-line grep find pcregrep
Rectangle 27 12

rush is a replacement for the unix shell (bash, zsh, etc) which uses pure Ruby syntax. Grep through files, find and kill processes, copy files - everything you do in the shell, now in Ruby

How do I use Ruby for shell scripting? - Stack Overflow

ruby shell scripting
Rectangle 27 572

Use the shell globbing syntax:

grep pattern -r --include=\*.{cpp,h} rootdir
--exclude

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 like grep pattern -r --include=foo.cpp --include=bar.h rootdir, which would only search files named foo.cpp and bar.h, which is quite likely not what you wanted.

grep pattern -r --include="*.{cpp,h}" rootdir

@topek: Good point -- if you have any .cpp/.h files in your current directory, then the shell will expand the glob before invoking grep, so you'll end up with a command line like grep pattern -r --include=foo.cpp --include=bar.h rootdir, which will only search files named foo.cpp or bar.h. If you don't have any files that match the glob in the current directory, then the shell passes on the glob to grep, which interprets it correctly.

I just realized that the glob is used to only matching the filename. To exclude a whole directory one needs --exclude-dir option. Same rules apply though. Only directory filename is matched, not a path.

--include doesn't seem to work after --exclude. I suppose it doesn't make sense to even try, except that I have an alias to grep with a long list of --exclude and --exclude-dir, which I use for searching code, ignoring libraries and swap files and things. I would've hoped that grep -r --exclude='*.foo' --include='*.bar' would work, so I could limit my alias to --include='*.bar' only, but it seems to ignore the --include and include everything that's not a .foo file. Swapping the order of the --include and --exclude works, but alas, that's not helpful with my alias.

unix - Use grep --exclude/--include syntax to not grep through certain...

unix search shell command-line grep
Rectangle 27 568

Use the shell globbing syntax:

grep pattern -r --include=\*.{cpp,h} rootdir
--exclude

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 like grep pattern -r --include=foo.cpp --include=bar.h rootdir, which would only search files named foo.cpp and bar.h, which is quite likely not what you wanted.

That's exactly what I was looking for, thanks =)

grep pattern -r --include="*.{cpp,h}" rootdir

@topek: Good point -- if you have any .cpp/.h files in your current directory, then the shell will expand the glob before invoking grep, so you'll end up with a command line like grep pattern -r --include=foo.cpp --include=bar.h rootdir, which will only search files named foo.cpp or bar.h. If you don't have any files that match the glob in the current directory, then the shell passes on the glob to grep, which interprets it correctly.

I just realized that the glob is used to only matching the filename. To exclude a whole directory one needs --exclude-dir option. Same rules apply though. Only directory filename is matched, not a path.

--include doesn't seem to work after --exclude. I suppose it doesn't make sense to even try, except that I have an alias to grep with a long list of --exclude and --exclude-dir, which I use for searching code, ignoring libraries and swap files and things. I would've hoped that grep -r --exclude='*.foo' --include='*.bar' would work, so I could limit my alias to --include='*.bar' only, but it seems to ignore the --include and include everything that's not a .foo file. Swapping the order of the --include and --exclude works, but alas, that's not helpful with my alias.

unix - Use grep --exclude/--include syntax to not grep through certain...

unix search shell command-line grep
Rectangle 27 548

Use the shell globbing syntax:

grep pattern -r --include=\*.{cpp,h} rootdir
--exclude

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 like grep pattern -r --include=foo.cpp --include=bar.h rootdir, which would only search files named foo.cpp and bar.h, which is quite likely not what you wanted.

grep pattern -r --include="*.{cpp,h}" rootdir

@topek: Good point -- if you have any .cpp/.h files in your current directory, then the shell will expand the glob before invoking grep, so you'll end up with a command line like grep pattern -r --include=foo.cpp --include=bar.h rootdir, which will only search files named foo.cpp or bar.h. If you don't have any files that match the glob in the current directory, then the shell passes on the glob to grep, which interprets it correctly.

I just realized that the glob is used to only matching the filename. To exclude a whole directory one needs --exclude-dir option. Same rules apply though. Only directory filename is matched, not a path.

--include doesn't seem to work after --exclude. I suppose it doesn't make sense to even try, except that I have an alias to grep with a long list of --exclude and --exclude-dir, which I use for searching code, ignoring libraries and swap files and things. I would've hoped that grep -r --exclude='*.foo' --include='*.bar' would work, so I could limit my alias to --include='*.bar' only, but it seems to ignore the --include and include everything that's not a .foo file. Swapping the order of the --include and --exclude works, but alas, that's not helpful with my alias.

unix - Use grep --exclude/--include syntax to not grep through certain...

unix search shell command-line grep
Rectangle 27 192

If you just want to skip binary files, I suggest you look at the -I (upper case i) option. It ignores binary files. I regularly use the following command:

grep -rI --exclude-dir="\.svn" "pattern" *
--exclude-dir

Any suggestions for what to use when --exclude-dir is unavailable? In all my attemps, --exclude does not appear to fit the bill.

You can always download the latest grep source from GNU, and do a 'configure; make; sudo make install'. This is one of the first things I do on a Mac or older Linunx distribution.

Exactly what I needed. Actually, I use git. So, --exclude-dir="\.git". :-)

unix - Use grep --exclude/--include syntax to not grep through certain...

unix search shell command-line grep
Rectangle 27 192

If you just want to skip binary files, I suggest you look at the -I (upper case i) option. It ignores binary files. I regularly use the following command:

grep -rI --exclude-dir="\.svn" "pattern" *
--exclude-dir

Any suggestions for what to use when --exclude-dir is unavailable? In all my attemps, --exclude does not appear to fit the bill.

You can always download the latest grep source from GNU, and do a 'configure; make; sudo make install'. This is one of the first things I do on a Mac or older Linunx distribution.

Exactly what I needed. Actually, I use git. So, --exclude-dir="\.git". :-)

unix - Use grep --exclude/--include syntax to not grep through certain...

unix search shell command-line grep
Rectangle 27 187

If you just want to skip binary files, I suggest you look at the -I (upper case i) option. It ignores binary files. I regularly use the following command:

grep -rI --exclude-dir="\.svn" "pattern" *
--exclude-dir

Any suggestions for what to use when --exclude-dir is unavailable? In all my attemps, --exclude does not appear to fit the bill.

You can always download the latest grep source from GNU, and do a 'configure; make; sudo make install'. This is one of the first things I do on a Mac or older Linunx distribution.

Exactly what I needed. Actually, I use git. So, --exclude-dir="\.git". :-)

unix - Use grep --exclude/--include syntax to not grep through certain...

unix search shell command-line grep
Rectangle 27 572

Use the shell globbing syntax:

grep pattern -r --include=\*.{cpp,h} rootdir
--exclude

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 like grep pattern -r --include=foo.cpp --include=bar.h rootdir, which would only search files named foo.cpp and bar.h, which is quite likely not what you wanted.

grep pattern -r --include="*.{cpp,h}" rootdir

@topek: Good point -- if you have any .cpp/.h files in your current directory, then the shell will expand the glob before invoking grep, so you'll end up with a command line like grep pattern -r --include=foo.cpp --include=bar.h rootdir, which will only search files named foo.cpp or bar.h. If you don't have any files that match the glob in the current directory, then the shell passes on the glob to grep, which interprets it correctly.

I just realized that the glob is used to only matching the filename. To exclude a whole directory one needs --exclude-dir option. Same rules apply though. Only directory filename is matched, not a path.

--include doesn't seem to work after --exclude. I suppose it doesn't make sense to even try, except that I have an alias to grep with a long list of --exclude and --exclude-dir, which I use for searching code, ignoring libraries and swap files and things. I would've hoped that grep -r --exclude='*.foo' --include='*.bar' would work, so I could limit my alias to --include='*.bar' only, but it seems to ignore the --include and include everything that's not a .foo file. Swapping the order of the --include and --exclude works, but alas, that's not helpful with my alias.

unix - Use grep --exclude/--include syntax to not grep through certain...

unix search shell command-line grep
Rectangle 27 55

Please take a look at ack, which is designed for exactly these situations. Your example of

grep -ircl --exclude=*.{png,jpg} "foo=" *

is done with ack as

ack -icl "foo="

because ack never looks in binary files by default, and -r is on by default. And if you want only CPP and H files, then just do

ack -icl --cpp "foo="

Looks nice, will try the standalone Perl version next time, thanks.

Good call, I can no longer live without ack.

stackoverflow.com/questions/667471/ - This will allow you to get ack on windows, if that is where you are running grep from.

@Chance Maybe you want silversearcher-ag, just apt-get in Ubuntu :)

unix - Use grep --exclude/--include syntax to not grep through certain...

unix search shell command-line grep
Rectangle 27 55

Please take a look at ack, which is designed for exactly these situations. Your example of

grep -ircl --exclude=*.{png,jpg} "foo=" *

is done with ack as

ack -icl "foo="

because ack never looks in binary files by default, and -r is on by default. And if you want only CPP and H files, then just do

ack -icl --cpp "foo="

Looks nice, will try the standalone Perl version next time, thanks.

Good call, I can no longer live without ack.

stackoverflow.com/questions/667471/ - This will allow you to get ack on windows, if that is where you are running grep from.

@Chance Maybe you want silversearcher-ag, just apt-get in Ubuntu :)

unix - Use grep --exclude/--include syntax to not grep through certain...

unix search shell command-line grep
Rectangle 27 55

Please take a look at ack, which is designed for exactly these situations. Your example of

grep -ircl --exclude=*.{png,jpg} "foo=" *

is done with ack as

ack -icl "foo="

because ack never looks in binary files by default, and -r is on by default. And if you want only CPP and H files, then just do

ack -icl --cpp "foo="

Looks nice, will try the standalone Perl version next time, thanks.

Good call, I can no longer live without ack.

stackoverflow.com/questions/667471/ - This will allow you to get ack on windows, if that is where you are running grep from.

@Chance Maybe you want silversearcher-ag, just apt-get in Ubuntu :)

unix - Use grep --exclude/--include syntax to not grep through certain...

unix search shell command-line grep
Rectangle 27 31

grep 2.5.3 introduced the --exclude-dir parameter which will work the way you want.

grep -rI --exclude-dir=\.svn PATTERN .

You can also set an environment variable: GREP_OPTIONS="--exclude-dir=.svn"

+1 for mentioning the exact version number; I have grep 2.5.1 and exclude-dir option is not available

but --exclude DOES NOT work for directories.

unix - Use grep --exclude/--include syntax to not grep through certain...

unix search shell command-line grep
Rectangle 27 31

grep 2.5.3 introduced the --exclude-dir parameter which will work the way you want.

grep -rI --exclude-dir=\.svn PATTERN .

You can also set an environment variable: GREP_OPTIONS="--exclude-dir=.svn"

+1 for mentioning the exact version number; I have grep 2.5.1 and exclude-dir option is not available

but --exclude DOES NOT work for directories.

unix - Use grep --exclude/--include syntax to not grep through certain...

unix search shell command-line grep