我经常使用find命令来搜索源代码、删除文件等等。令人烦恼的是,由于Subversion在.svn/text-base/目录中存储了每个文件的副本,我的简单搜索最终会得到大量重复的结果。例如,我想在多个messages.h和messages.cpp文件中递归搜索uint:

# find -name 'messages.*' -exec grep -Iw uint {} +
./messages.cpp:            Log::verbose << "Discarding out of date message: id " << uint(olderMessage.id)
./messages.cpp:    Log::verbose << "Added to send queue: " << *message << ": id " << uint(preparedMessage->id)
./messages.cpp:                Log::error << "Received message with invalid SHA-1 hash: id " << uint(incomingMessage.id)
./messages.cpp:            Log::verbose << "Received " << *message << ": id " << uint(incomingMessage.id)
./messages.cpp:            Log::verbose << "Sent message: id " << uint(preparedMessage->id)
./messages.cpp:        Log::verbose << "Discarding unsent message: id " << uint(preparedMessage->id)
./messages.cpp:        for (uint i = 0; i < 10 && !_stopThreads; ++i) {
./.svn/text-base/messages.cpp.svn-base:            Log::verbose << "Discarding out of date message: id " << uint(olderMessage.id)
./.svn/text-base/messages.cpp.svn-base:    Log::verbose << "Added to send queue: " << *message << ": id " << uint(preparedMessage->id)
./.svn/text-base/messages.cpp.svn-base:                Log::error << "Received message with invalid SHA-1 hash: id " << uint(incomingMessage.id)
./.svn/text-base/messages.cpp.svn-base:            Log::verbose << "Received " << *message << ": id " << uint(incomingMessage.id)
./.svn/text-base/messages.cpp.svn-base:            Log::verbose << "Sent message: id " << uint(preparedMessage->id)
./.svn/text-base/messages.cpp.svn-base:        Log::verbose << "Discarding unsent message: id " << uint(preparedMessage->id)
./.svn/text-base/messages.cpp.svn-base:        for (uint i = 0; i < 10 && !_stopThreads; ++i) {
./virus/messages.cpp:void VsMessageProcessor::_progress(const string &fileName, uint scanCount)
./virus/messages.cpp:ProgressMessage::ProgressMessage(const string &fileName, uint scanCount)
./virus/messages.h:    void _progress(const std::string &fileName, uint scanCount);
./virus/messages.h:    ProgressMessage(const std::string &fileName, uint scanCount);
./virus/messages.h:    uint        _scanCount;
./virus/.svn/text-base/messages.cpp.svn-base:void VsMessageProcessor::_progress(const string &fileName, uint scanCount)
./virus/.svn/text-base/messages.cpp.svn-base:ProgressMessage::ProgressMessage(const string &fileName, uint scanCount)
./virus/.svn/text-base/messages.h.svn-base:    void _progress(const std::string &fileName, uint scanCount);
./virus/.svn/text-base/messages.h.svn-base:    ProgressMessage(const std::string &fileName, uint scanCount);
./virus/.svn/text-base/messages.h.svn-base:    uint        _scanCount;

我怎么能告诉find忽略。svn目录?


更新:如果您将SVN客户端升级到1.7版本,这就不再是一个问题。

Subversion 1.7中引入的更改的一个关键特性是将工作副本元数据存储集中到单个位置。Subversion 1.7工作副本只有一个.svn目录,而不是在工作副本的每个目录中都有一个.svn目录——在工作副本的根目录。这个目录包括一个sqlite支持的数据库,其中包含Subversion工作副本所需的所有元数据。


当前回答

这在Unix提示符中对我来说是有效的

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

上面的命令将列出不包含.svn的文件,并执行您提到的grep。

其他回答

为此,我使用grep。把它放在~/.bashrc中

export GREP_OPTIONS="--binary-files=without-match --color=auto --devices=skip --exclude-dir=CVS --exclude-dir=.libs --exclude-dir=.deps --exclude-dir=.svn"

Grep在调用时自动使用这些选项

我只是想给Kaleb和其他人的帖子(详细介绍了find -prune选项,ack, repofind命令等的使用)添加一个简单的替代方案,这特别适用于你在问题中描述的用法(以及任何其他类似的用法):

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

要忽略.svn, .git和其他隐藏目录(以点开始),尝试:

find . -type f -not -path '*/\.*'

但是,如果使用find的目的是在文件中搜索,您可以尝试使用这些命令:

git grep -专门设计的命令,用于在git存储库中搜索模式。 Ripgrep -默认情况下忽略隐藏文件和.gitignore中指定的文件。

相关:如何在Linux上找到包含特定文本的所有文件?

找到。| grep -v \.svn

为什么不使用grep管道命令,这很容易理解:

your find command| grep -v '\.svn'