3

I am trying to find and delete files in current directory and subdirectories (recursively) which match different patterns and print the matching files to stdout to know which ones are deleted.

For example I want to match all files starting with '&' and all files starting and ending with '$'.
I've tried using:

find . -type f -name '&*' -or -type f -name '$*$' -exec rm -v {} \;

but rm apply only on the last match ('$*$').
Thus i've tried :

find . -type f -name '&*' -or -type f -name '$*$' -delete

But this not only match only the last pattern but it doesn't output the deleted files.
I know I can do this:

rm -v ``find -type f -name '&*' -or -type f -name '$*$'``

but I would really like to avoid this kind of approach and do it with find command.

Any tips?
Thanks in advance.

LotoLo
  • 606

2 Answers2

3

You problem is that exec only applies to the second pattern, try putting parenthesis around your search conditions, to fix that:

find . \( -type f -name '&*' -or -type f -name '$*$' \) -exec rm -v {} \;

The thing to note is that this is not a bug, but a feature, so that you can do something like that:

find -type f -name '&*' -exec mv '{}' ./backup ';' -or -type f -name '$*$' -exec rm -v '{}' ';'

if you need to

zeppelin
  • 3,822
  • 10
  • 21
0

Though zeppelin answer is correct, I found also this solution:

find . -type f \( -name '&*' -or -name '$*$' \) -print -delete

LotoLo
  • 606