Using GNU findutils, I need to search a directory tree for a certain file. If the file has been found for a given branch, I want to prevent find from recursing further into the branch. Say I want to find the file foo, and this is my directory tree:
├── a
│ ├── a1
│ │ └── foo
│ └── foo
├── b
└── c
└── foo
Given I am searching the tree above, I want to find a/foo and c/foo. However, I don't want to find a/a1/foo since I already found foo in a parent directory to a1. It seems I should use the -prune flag to the find command and I found this link https://unix.stackexchange.com/questions/24557/how-do-i-stop-a-find-from-descending-into-found-directories, for example, but I cannot make it work. My attempts include:
$ find -name foo -type f -prune
./a/a1/foo <- Unwanted hit
./a/foo
./c/foo
and
$ find -name foo -type f -prune -exec find ../foo -type f {} \;
find: paths must precede expression: ./a/a1/foo
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
find: paths must precede expression: ./a/foo
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
find: paths must precede expression: ./c/foo
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
./a/foo
the unwanted hit? Are we interested in that hit that is less deeper? What ifmkdir -p a/a1 a/b1; touch a/a1/foo a/b1/foo
. Then isa/a1/foo
ora/b1/foo
the unwanted hit? Or it doesn't matter, at least one should do it, no matter where? – Impartiala/a1/foo
anda/b1/foo
should be returned. – Eggshaped