SVN Tip: Find/Grep but excluding .svn (Linux)

Here I give Linux commands to allow the searching for filenames or strings within files without descending into the <.svn> subdirectories.

The following, in a file called <>:


### Find and Grep for Working Copies

### $1 = start directory
### $2 = file glob
### $3 = string pattern

if [ "$3" == "" ] ; then
 echo "working-copy find and grep"
 echo "syntax: ./wc_find_grep \${start_dir} \${file_glob} \${string_pattern}"
 find "$1" -regextype posix-extended \
 -regex ".*(\.svn)" \
 -prune -or \
 -iname "$2" \
 -exec grep --with-filename --line-number "$3" \{\} \;

may be used to find strings within files with names matching a certain pattern, using a command

./ . "filename_glob" "string_regex"

(You may need to type

chmod a+x

to ensure that the script has appropriate execution attributes.)

Similarly, the simpler lines, in a file called <>:


### Find for Working Copies

### $1 = start directory
### $2 = file glob

if [ "$2" == "" ] ; then
 echo "working-copy find"
 echo "syntax: ./wc_find \${start_dir} \${file_glob}"
 find "$1" -regextype posix-extended \
 -regex ".*(\.svn)" \
 -prune -or \
 -iname "$2" \

may be used to perform just the filename search.

(Or, you might prefer to have both functions in the same script.)



3 Responses to “SVN Tip: Find/Grep but excluding .svn (Linux)”

  1. yut45390 Says:

    I find

    find $1 -type d -name ‘.svn’ -prune -o -type f -name $2 -print

    superior, because your version shows ‘.svn’ directory entries.

  2. Rob Says:

    @yut45390: The whole point of my `find` command is to exclude the ‘.svn’ directories. I don’t see a significant difference between your example and mine; both prune the ‘.svn’ directories. My command does not show the ‘.svn’ directories. Could you provide an example (of $2) where the two commands differ qualitatively?

    Your command is certainly a little simpler when $2 is simple, and is perhaps more appropriate to direct command-line entry; my command is tailored to use in its script context.

  3. Paul Smart (@paulsmart) Says:

    Simple one for finding strings in files is…
    find . -type f -exec grep -l ‘string’ {} \;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: