how to delete all files with specific extension in specific named folders in large tree?

I would execute a find inside another find. For example, I would execute this command line in order to list the files that would be removed:

$ find /path/to/source -type d -name 'rules' -exec find '{}' -mindepth 1 -maxdepth 1 -type f -iname '*.pdf' -print ';'

Then, after checking the list, I would execute:

$ find /path/to/source -type d -name 'rules' -exec find '{}' -mindepth 1 -maxdepth 1 -type f -iname '*.pdf' -print -delete ';'

With a shell that supports extended globs and null globs e.g. zsh:

for d in ./**/rules/
do
set -- ${d}*.pdf(N)                               
(( $# > 0 )) && printf %s\\n $@
done

or bash:

shopt -s globstar
shopt -s nullglob
for d in ./**/rules/
do
set -- "${d}"*.pdf
(( $# > 0 )) && printf %s\\n "$@"
done

replace printf %s\\n with rm if you're happy with the result.


Since you are on gnu/linux you could also run:

find . -type f -regextype posix-basic -regex '.*/rules/[^/]*.pdf' -delete

remove -delete if you want to perform a dry-run.

Tags:

Find

Files