Search code examples
bashshellsedpipefind-util

Why is my `find` command giving me errors relating to ignored directories?


I have this find command:

find . -type f  -not -path '**/.git/**' -not -path '**/node_modules/**'  | xargs sed -i '' s/typescript-library-skeleton/xxx/g;

for some reason it's giving me these warnings/errors:

find: ./.git/objects/3c: No such file or directory
find: ./.git/objects/3f: No such file or directory
find: ./.git/objects/41: No such file or directory

I even tried using:

-not -path '**/.git/objects/**'

and got the same thing. Anybody know why the find is searching in the .git directory? Seems weird.


Solution

  • why is the find searching in the .git directory?

    GNU find is clever and supports several optimizations over a naive implementation:

    • It can flip the order of -size +512b -name '*.txt' and check the name first, because querying the size will require a second syscall.
    • It can count the hard links of a directory to determine the number of subdirectories, and when it's seen all it no longers needs to check them for -type d or for recursing.
    • It can even rewrite (-B -or -C) -and -A so that if the checks are equally costly and free of side effects, the -A will be evaluated first, hoping to reject the file after 1 test instead of 2.

    However, it is not yet clever enough to realize that -not -path '*/.git/*' means that if you find a directory .git then you don't even need to recurse into it because all files inside will fail to match.

    Instead, it dutifully recurses, finds each file and matches it against the pattern as if it was a black box.

    To explicitly tell it to skip a directory entirely, you can instead use -prune. See How to exclude a directory in find . command