2

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.

2 Answers 2

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

1
  • here's where the parenthesis come in handy! This was so simple, thanks!
    – LotoLo
    Oct 29, 2016 at 18:48
0

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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .