views:

787

answers:

6

I need to search all cpp/h files in svn working copy for "foo", excluding svn's special folders completely. What is the exact command for GNU grep?

+8  A: 

I use ack for this purpose, it's like grep but automatically knows how to exclude source control directories (among other useful things).

Greg Hewgill
Note that the ack package on Ubuntu/Debian is actually called "ack-grep", since there is a Kanji code converter called "ack".
JesperE
+1  A: 

This is a RTFM. I typed 'man grep' and '/exclude' and got:

--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.

--exclude-from=FILE Skip files whose base name matches any of the file-name globs read from FILE (using wildcard matching as described under --exclude).

--exclude-dir=DIR Exclude directories matching the pattern DIR from recursive searches.

Frentos
RTFMing people is something you do on usenet. SO is different. By the way, you didn't answer the question.
Constantin
+5  A: 

grep -ir --exclude-dir=.svn foo *

In the working directory will do. Omit the 'i' if you want the search to be case sensitive.

If you want to check only .cpp and .h files use

grep -ir --include={.cpp,.h} --exclude-dir=.svn foo *

Corporal Touchy
Any chance to replace * with something that only matches cpp/h?
Constantin
Sorry, can't find --exclude-dir option: http://unixhelp.ed.ac.uk/CGI/man-cgi?grep
Constantin
it's not available in all versions of grep. But on my ubuntu 8.04 system it's there at least
Isak Savo
+2  A: 

Going a little off-topic:

If you have a working copy with a lot of untracked files (i.e. not version-controlled) and you only want to search source controlled files, you can do

svn ls -R | xargs -d '\n' grep <string-to-search-for>
JesperE
+1  A: 

I wrote this script which I've added to my .bashrc. It automatically excludes SVN directories from grep, find and locate.

Simon Howard
what about real find and locate?
Corporal Touchy
+1  A: 

I use these bash aliases for grepping for content and files in svn trees... I find it faster and more pleasant to search from the commandline (and use vim for coding) rather than a GUI-based IDE:

s () {
    local PATTERN=$1
    local COLOR=$2
    shift; shift;
    local MOREFLAGS=$*

    if  ! test -n "$COLOR" ; then
        # is stdout connected to terminal?
        if test -t 1; then
            COLOR=always
        else
            COLOR=none
        fi
    fi

    find -L . \
        -not \( -name .svn -a -prune \) \
        -not \( -name templates_c -a -prune \) \
        -not \( -name log -a -prune \) \
        -not \( -name logs -a -prune \) \
        -type f \
        -not -name \*.swp \
        -not -name \*.swo \
        -not -name \*.obj \
        -not -name \*.map \
        -not -name access.log \
        -not -name \*.gif \
        -not -name \*.jpg \
        -not -name \*.png \
        -not -name \*.sql \
        -not -name \*.js \
        -exec grep -iIHn -E --color=${COLOR} ${MOREFLAGS} -e "${PATTERN}" \{\} \;
}

# s foo | less
sl () {
    local PATTERN=$*
    s "$PATTERN" always | less
}

# like s but only lists the files that match
smatch () {
    local PATTERN=$1
    s $PATTERN always -l
}

# recursive search (filenames) - find file
f () {
    find -L . -not \( -name .svn -a -prune \) \( -type f -or -type d \) -name "$1"
}
Ether