Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prevent find from printing .git folders?

Tags:

find

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 the skipped .git directories from printing to the standard output?

like image 848
Nathan Neff Avatar asked May 13 '10 05:05

Nathan Neff


2 Answers

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;

like image 100
eMPee584 Avatar answered Nov 13 '22 03:11

eMPee584


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*"
like image 4
Toni Homedes i Saun Avatar answered Nov 13 '22 01:11

Toni Homedes i Saun