Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

One of my favorite BASH commands is:

find . -name '*.*' -exec grep 'SearchString' {} /dev/null \;

which searches the contents of all of the files at and below the current directory for the specified SearchString. As a developer, this has come in handy at times.

Due to my current project, and the structure of my codebase, however, I'd like to make this BASH command even more advanced by not searching any files that are in or below a directory that contains ".svn", or any files that end with ".html"

The MAN page for find kind of confused me though. I tried using -prune, and it gave me strange behavior. In an attempt to skip only the .html pages (to start), I tried :

find . -wholename './*.html' -prune -exec grep 'SearchString' {} /dev/null \;

and did not get the behavior I was hoping for. I think I might be missing the point of -prune. Could you guys help me out?

Thanks

share|improve this question
    
Just fyi: find is not a build-in bash command but a separate program –  WakiMiko Mar 6 '12 at 0:54

3 Answers 3

up vote 13 down vote accepted

You can use the negate (!) feature of find to not match files with specific names:

find . ! -name '*.html' ! -name '*.svn*' -exec grep 'SearchString' {} /dev/null \;

So if the name ends in .html or contains .svn anywhere in the path, it will not match, and so the exec will not be executed.

share|improve this answer
    
Should I still specify -name '.' somewhere in there? Would I do that before, or after the negations? –  Cody S Mar 6 '12 at 1:21
    
Was the intention of your *.* match to ensure it only matched files containing a .? Find will match all files in the absence of a name directive, so the above will match everything except html and svn –  Paul Mar 6 '12 at 1:23

The following find command does prune directories whose names contain .svn, Although it does not descend into the directory, the pruned path name is printed ...(-name '*.svn' is the cause!) ..

You can filter out the directory names via: grep -d skip which silently skips such input "directory names".

With GNU grep, you can use -H instead of /dev/null. As a slight side issue: \+ can be much faster than \;, eg. for 1 million one-line files, using \; it took 4m20s, using \+ it took only 1.2s.

The following method uses xargs instead of -exec, and assumes there are no newlines \n in any of your file names. As used here, xargs is much the same as find's \+.

xargs can pass file-names which contain consecutive spaces by changing the input delimiter to '\n' with the -d option.

This excludes directories whose names contain .svn and greps only files which don't end with .html.

find . \( -name '*.svn*' -prune  -o ! -name '*.html' \) |
   xargs -d '\n' grep -Hd skip 'SearchString'
share|improve this answer
    
Thank you for pointing out the \+ variant of the -exec action. Hooray for slight side issues! –  Christian Long Sep 12 at 18:10

I've had the same issue for a long time, and there are several solutions which can be applicable in different situations:

  • ack-grep is a sort of "developer's grep" which by default skips version control directories and temporary files. The man page explains how to search only specific file types and how to define your own.
  • grep's own --exclude and --exclude-dir options can be used very easily to skip file globs and single directories (no globbing for directories, unfortunately).
  • find . \( -type d -name '.svn' -o -type f -name '*.html' \) -prune -o -print0 | xargs -0 grep ... should work, but the above options are probably less of a hassle in the long run.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.