find a -not -path "*/b*" -type f -delete
note that this will not delete any file which name is starting with
b and also any file which has any parent directory which name is starting with
i assume that you used
b as placeholders and the real names are more like
foobalator. if you put
foobalator in place of
b then the amount of false positives will be drastically reduced.
there is still chance for false positives though, but at least they will be not deleted, so it will be not as tragic. furthermore, you can check for false positives by first running the command without
-depth causes find to process the contents of a directory before the directory itself which affects the outcome drastically. if you run the command without
-delete you should explicitly add
find a -depth -not -path "*/b*" -type f
-path is required instead of
-name only matches against the basename while
-path is matching against the entire pathname.
-depth (implied by -delete) causes find to process the contents of a directory before the directory itself. this is needed so
-delete can delete the directory from inside out.
-delete refuses to delete non-empty directories.
-prune prevents find from processing the contents of a directory based on some filters. the way
-depth is implemented in find causes
-prune to be ineffective when
-depth is also in effect.