开发者

How do I prevent find from printing .git folders?

I have a find command that I run to find files whose names contain foo.

I want to skip the .git directory. The command below works except it prints an annoying .git any time it skips a .git directory:

find . ( -name .git ) -prune -o -name '*foo*'

How can I prevent开发者_如何学Go the skipped .git directories from printing to the standard output?


So just for better visibility:

find -name '.git*' -prune -o -name '*foo*' -print

This also omits .gitignore files; note the trailing -print to omit printing, -prune stops descending into it but without -print prints it nevertheless. Twisted C;


find . -not -wholename "./.git*" -name "*foo*"

or, more strictly, if you don't want to see .git/ but do want to search in other dirs whose name also begins with .git (.git-foo/bar/...)

find . -not -wholename "./.git" -not -wholename "./.git/*" -name "*foo*"

If your .git/ directories may not always necessarily be located at the top-level of your search directory, you will want to use -not -wholename ".*/.git" and -not -wholename ".*/.git/*".

A bit odder but more efficient because it prunes the whole .git dir:

find . -not \( -wholename "./.git" -prune \) -name "*foo*"


Try this one:

find . -name '*foo*' | grep -v '\.git'

This will still traverse into the .git directories, but won't display them. Or you can combine with your version:

find . ( -name .git ) -prune -o -name '*foo*' | grep -v '\.git'

You can also do it without grep:

find . ( -name .git ) -prune -printf '' -o -name '*foo*' -print
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜