How can I find a directory with a certain name but only if it is in another directory with a certain name? For example when I have the following directory structure
a
├── b
│ └── e
├── c
│ └── e
└── d
I'd like to find the directory 'e', but only when it is located in a directory called 'c'. If possible with the find command alone without using grep.
-path
is specified in POSIX find. – Quasímodo Nov 01 '20 at 14:34e
anywhere withinc
, right? Not just immediately below. I guess the same could be done with-path
with something like-path '*/c/*/e' -o -path '*/c/e'
. (Can't seem to do it with just one pattern.) – ilkkachu Nov 01 '20 at 20:39e
anywhere in a directory tree underc
. – terdon Nov 02 '20 at 09:23-maxdepth 1
in POSIXfind
see this answer. – Ruslan Nov 02 '20 at 15:53maxdepth 1
here? The whole point of this question is to make it recursive. If we wanted-maxdepth 1
we could just doecho */c/e
. – terdon Nov 02 '20 at 15:54-maxdepth 1
is the solution. – Ruslan Nov 02 '20 at 15:58-prune
magic to be done here, but I admit I never use-prune
so I don't really know how to. – terdon Nov 02 '20 at 16:09find . -name 'c' -exec find '{}/e' -type d \( -name 'e' -ls -o -prune \) \; 2>/dev/null
: it seems to work on my machine (to test, I also added files under each directories as otherwise some solutions would list also files under any "c/e" subdirs). to test: in a subdir:mkdir -p a b c a/b a/c a/c/e a/c/d/e a/c/d/e/c/e/f/g ; for i in $(find */ -type d -ls); do ( cd "$i" && touch a b c d e ) ; done
(creates wherever it can up to five files under each subdir, except those matching a dir name) (I corrected the previous comment) – Olivier Dulac Nov 02 '20 at 17:04-prune
is deep dark magic for me, I wouldn't dare write an answer with it! :) – terdon Nov 02 '20 at 17:17