Tried to create a script to delete files from the directory in a first script argument that does not contain a sequence of symbols in a second script argument. But It doesn't do a thing. What I tried:

find $1 -name !\(.*$2.*\) | tee file | cat

Where am I wrong?

  • Well, first thing first, -name is a substring, not regex, match. Try -regex or -iregex instead. – a CVn Feb 23 '17 at 12:43

Use -regex for a regex, not -name. Use -! or -not for negation:

find "$1" -not -regex ".*$2.*"

Quoting the variables should be safer than using them unquoted.

| improve this answer | |
  • Wow. It works. -regex, heh... – Kosarar Feb 23 '17 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.