4

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?

Ether
  • 53,118
  • 13
  • 86
  • 159
Constantin
  • 27,478
  • 10
  • 60
  • 79
  • As Frentos illustrates, your question would have been easily answered had you took the time to consult the man pages. – freespace Oct 16 '08 at 10:55
  • 2
    Hey freespace, why not just answer the "easy" question? I would gladly accept your answer if it's correct. – Constantin Oct 21 '08 at 16:19

6 Answers6

9

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
  • 951,095
  • 183
  • 1,149
  • 1,285
  • 2
    Note that the ack package on Ubuntu/Debian is actually called "ack-grep", since there is a Kanji code converter called "ack". – JesperE Oct 16 '08 at 11:08
7

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 *

Sarien
  • 6,647
  • 6
  • 35
  • 55
3

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
  • 63,317
  • 21
  • 138
  • 197
1

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
  • 53,118
  • 13
  • 86
  • 159
1

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
  • 169
  • 1
  • 3
  • 1
    RTFMing people is something you do on usenet. SO is different. By the way, you didn't answer the question. – Constantin Oct 16 '08 at 11:43
1

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

Simon Howard
  • 8,999
  • 5
  • 28
  • 21