How can I get `find` to ignore .svn directories?

后端 未结 20 1433
迷失自我
迷失自我 2020-12-04 05:05

I often use the find command to search through source code, delete files, whatever. Annoyingly, because Subversion stores duplicates of each file in its .

相关标签:
20条回答
  • 2020-12-04 05:35

    Here is what I would do in your case:

    find . -path .svn -prune -o -name messages.* -exec grep -Iw uint {} +
    

    Emacs' rgrep built-in command ignores .svn directory, and many more files you're probably not interested in when performing a find | grep. Here is what it uses by default:

    find . \( -path \*/SCCS -o -path \*/RCS -o -path \*/CVS -o -path \*/MCVS \
              -o -path \*/.svn -o -path \*/.git -o -path \*/.hg -o -path \*/.bzr \
              -o -path \*/_MTN -o -path \*/_darcs -o -path \*/\{arch\} \) \
         -prune -o \
           \( -name .\#\* -o -name \*.o -o -name \*\~ -o -name \*.bin -o -name \*.lbin \
              -o -name \*.so -o -name \*.a -o -name \*.ln -o -name \*.blg \
              -o -name \*.bbl -o -name \*.elc -o -name \*.lof -o -name \*.glo \
              -o -name \*.idx -o -name \*.lot -o -name \*.fmt -o -name \*.tfm \
              -o -name \*.class -o -name \*.fas -o -name \*.lib -o -name \*.mem \
              -o -name \*.x86f -o -name \*.sparcf -o -name \*.fasl -o -name \*.ufsl \
              -o -name \*.fsl -o -name \*.dxl -o -name \*.pfsl -o -name \*.dfsl \
              -o -name \*.p64fsl -o -name \*.d64fsl -o -name \*.dx64fsl -o -name \*.lo \
              -o -name \*.la -o -name \*.gmo -o -name \*.mo -o -name \*.toc \
              -o -name \*.aux -o -name \*.cp -o -name \*.fn -o -name \*.ky \
              -o -name \*.pg -o -name \*.tp -o -name \*.vr -o -name \*.cps \
              -o -name \*.fns -o -name \*.kys -o -name \*.pgs -o -name \*.tps \
              -o -name \*.vrs -o -name \*.pyc -o -name \*.pyo \) \
         -prune -o \
         -type f \( -name pattern \) -print0 \
         | xargs -0 -e grep -i -nH -e regex
    

    It ignores directories created by most version control systems, as well as generated files for many programming languages. You could create an alias that invokes this command and replace find and grep patterns for your specific problems.

    0 讨论(0)
  • 2020-12-04 05:36

    find . | grep -v \.svn

    0 讨论(0)
  • 2020-12-04 05:39

    This works for me in the Unix prompt

    gfind . \( -not -wholename '*\.svn*' \) -type f -name 'messages.*' -exec grep -Iw uint {} +

    The command above will list FILES that are not with .svn and do the grep you mentioned.

    0 讨论(0)
  • 2020-12-04 05:40

    Why dont you pipe your command with grep which is easily understandable:

    your find command| grep -v '\.svn'
    
    0 讨论(0)
  • 2020-12-04 05:40

    In a source code repository, I generally want to do things only to the text files.

    The first line is all files, excluding CVS, SVN, and GIT repository files.

    The second line excludes all binary files.

    find . -not \( -name .svn -prune -o -name .git -prune -o -name CVS -prune \) -type f -print0 | \
    xargs -0 file -n | grep -v binary | cut -d ":" -f1
    
    0 讨论(0)
  • 2020-12-04 05:42

    Just thought I'd add a simple alternative to Kaleb's and others' posts (which detailed the use of the find -prune option, ack, repofind commands etc.) which is particularly applicable to the usage you have described in the question (and any other similar usages):

    1. For performance, you should always try to use find ... -exec grep ... + (thanks Kenji for pointing this out) or find ... | xargs egrep ... (portable) or find ... -print0 | xargs -0 egrep ... (GNU; works on filenames containing spaces) instead of find ... -exec grep ... \;.

      The find ... -exec ... + and find | xargs form does not fork egrep for each file, but rather for a bunch of files at a time, resulting in much faster execution.

    2. When using the find | xargs form you can also use grep to easily and quickly prune .svn (or any directories or regular expression), i.e. find ... -print0 | grep -v '/\.svn' | xargs -0 egrep ... (useful when you need something quick and can't be bothered to remember how to set up find's -prune logic.)

      The find | grep | xargs approach is similar to GNU find's -regex option (see ghostdog74's post), but is more portable (will also work on platforms where GNU find is not available.)

    0 讨论(0)
提交回复
热议问题