Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.
find /full/path/dir -name '*.txt' -exec /bin/rm {} \;

Fine in a shell, but pop it in a sh script along with some similar lines, to get it to run nightly from a cronjob, and it reports:

find: missing argument to `-exec'

on everything. I've tried backslashes and quote-marks in possibly every combination. How can I make this work?

share|improve this question
add comment

3 Answers 3

up vote 2 down vote accepted

You probably have Windows line endings in your script file. Try running dos2unix on it to convert them.

You can reproduce this error at a shell prompt with:

$ find ... \;^M

Where you add "^M" by pressing Ctrl-v Ctrl-m

share|improve this answer
    
Perfect! I simply used "fromdos <file>" and everything is working perfectly. I don't know why the fact I'd also tried xargs got edited out of my original post, you've solved it with a simple solution. Many thanks again. –  talkingnews Jan 27 '11 at 20:29
    
@talkingnews: I don't know why @larsks was a bit overzealous with the editing. I'm glad I could help. –  Dennis Williamson Jan 27 '11 at 22:26
add comment

I can't imagine why

find /full/path/dir -name '*.txt' -print0 | xargs -0 rm

wouldn't work. To test your cronjob, do something like this:

find /full/path/to/dir -name '*.txt' -print0 | xargs -0 >/tmp/logfile

which will just echo what xargs would delete into /tmp/logfile so you can check manually.

share|improve this answer
    
Damn Phil, you're too fast ;) –  lynxman Jan 27 '11 at 19:19
add comment

This error happens when find doesn't match any files with that criteria, then tries to execute rm with an empty stack and it returns that error.

Phil suggestion is amazing though, so please mark him up! :)

share|improve this answer
    
ha you came up with the correct reason for why it fails sometimes so +1! In general I avoide -exec. –  Phil Hollenback Jan 27 '11 at 19:37
    
find won't try to execute the -exec if there are no results. –  Dennis Williamson Jan 27 '11 at 19:39
    
same here, I so much prefer your method through xargs, it's way more elegant –  lynxman Jan 27 '11 at 19:39
    
I had that issue several times Dennis, when you use -name and it returns empty it tries to return -exec, which is annoying as hell :/ –  lynxman Jan 27 '11 at 19:40
    
find -name '' -exec echo something {} \; doesn't print anything. Can you give a specific example of what you're describing? –  Dennis Williamson Jan 27 '11 at 19:46
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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