I am trying to delete all sub directories (and files inside) that do not contain one of two patterns in the directory name using Bash on OSX. I am trying to do this recursively - for a bunch of parent directories.
Lets say the pattern in sub dirs I want to not delete is either "foobar" or "doobar"
I have a whole bunch of Parent directories, like this one:
Parent001_dir 001_boo 002_hugs 003_foobar 004_doobar 005_apple ...
I want to run a for loop code that will go into each and every parent dir and delete the non-matching sub dirs.
Code I thought could work:
for i in /path/to/parent/directories/ do find !-name "*foobar*" -o !-name "*doobar*" -delete done
But I read the find manual and see no option for not matching patterns; !-name isn't an option.
EDIT- from comments, trying:
find /path/to/parent/directories/ -type d -not -name "*foobar*" -not -name "*doobar*" -delete
and getting a"path potentially not safe" message
If I find a way to fix my code, I could finally get "Parent001_dir" and all other parent dirs to look like:'
Parent001_dir 003_foobar 004_doobar
Are there wildcard characters that could fix my code maybe? Or should I use another approach entirely? Thanks in advance for any help.