If I run this command...

sudo find /storage -name "*~" -or -name ".*~" -or -name "#.*#"
-or -name ".DS_Store" -or -name "Thumbs.db"

... it gives me a list of matching files, as expected. However, if I use this command...

sudo find /storage -name "*~" -or -name ".*~" -or -name "#.*#"
-or -name ".DS_Store" -or -name "Thumbs.db" -exec rm -v {} \;

... nothing is deleted. Similarly, with echo, nothing is printed...

sudo find /storage -name "*~" -or -name ".*~" -or -name "#.*#"
-or -name ".DS_Store" -or -name "Thumbs.db" -exec echo {} \;

How come?

link|improve this question

59% accept rate
feedback

2 Answers

up vote 2 down vote accepted

You've got to group your expression correctly - currently the -exec only applies to the last -or branch.

sudo find /storage \( -name "*~" -or -name ".*~" -or -name "#.*#" -or -name ".DS_Store" -or -name "Thumbs.db" \) -exec rm -v {} \;

Just remember that -exec is just an expression that returns true if the command returns zero, so running the command is just a side-effect.

link|improve this answer
+1 Perfect! Thanks very much. – nbolton Jan 12 '10 at 22:16
especially note the escaped parentheses \( and \) -- this prevents the shell from interpreting them; in this case you want find to handle them as part of the expression. this and other good examples on the find manpage: linux.about.com/od/commands/l/blcmdl1_find.htm – quack quixote Jan 12 '10 at 22:16
Seems my workaround wasn't necessary, works like a charm, cheers. – Jeffrey Vandenborne Jan 12 '10 at 23:02
feedback
find Documents/ -iname '*.txt' -exec echo {} \;
 -or -iname '*.cpp' -exec echo {} \;

This however works. Hope this can help you further a little. If you use exec separately with all -or's the command will work.

I've found an alternative to your problem though.

for file in $(find Documents/ -iname '*.txt' 
-or -iname '*.cpp'| awk '{print $1}'); do rm $file; done
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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