0

I have the following find command and I'm surprised to see .git directories being found. Why?

$ find . ! -name '*git*' | grep git 
./.git/hooks
./.git/hooks/commit-msg
./.git/hooks/applypatch-msg.sample
./.git/hooks/prepare-commit-msg.sample
./.git/hooks/pre-applypatch.sample
./.git/hooks/commit-msg.sample
./.git/hooks/post-update.sample
nietonfir
  • 4,797
  • 6
  • 31
  • 43
Ankur Agarwal
  • 23,692
  • 41
  • 137
  • 208

4 Answers4

2

Because find searches for files and none of the found files have the search pattern in their name (see the man page). You need to remove the offending directory via the -prune switch:

find . -path ./.git -prune -o -not -name '*git*' -print |grep git

See Exclude directory from find . command

[edit] An alternative without -prune (and much more natural imho):

find . -not -path "*git*" -not -name '*git*' |grep git
Community
  • 1
  • 1
nietonfir
  • 4,797
  • 6
  • 31
  • 43
1

You're just seeing expected behaviour of find. The -name test is only applied to the filename itself, not the whole path. If you want to search everything but the .git directory, you can use bash(1)'s extglob option:

$ shopt -s extglob
$ find !(.git)
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
1

It doesn't really find those git-files. Instead it finds files under ./.git/ that match the pattern ! -name '*git*' which includes all files that don't include git in their filename (not path name).
Finds -name is about the files, not the path.

Try -iwholename instead of -name:
find . ! -iwholename '*git*'

wullxz
  • 17,830
  • 8
  • 32
  • 51
0

This is what I needed:

find . ! -path '*git*'
Ankur Agarwal
  • 23,692
  • 41
  • 137
  • 208