3
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?

larsks
  • 43,623
  • 14
  • 121
  • 180
talkingnews
  • 67
  • 1
  • 8

3 Answers3

2

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.

Phil Hollenback
  • 14,947
  • 4
  • 35
  • 52
2

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

Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151
  • Perfect! I simply used "fromdos " 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
1

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! :)

lynxman
  • 9,397
  • 3
  • 25
  • 28
  • 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
  • I can reproduce that on a linux environment by having * in the -name ... but here's the funny thing, I can only reproduce on RHEL, on some Ubuntu servers I have it's working fine (grr annoying) I reckon it can be due to env values? – lynxman Jan 27 '11 at 19:51
  • Are you quoting the argument to `-name`? If you do `-name foo*`, for example, you can get unexpected behavior since the glob is expanded by the shell. To delay that so it's expanded by `find` do `find -name 'foo*'`. Also, reply to comments using @Dennis so I'm automatically notified of the reply. – Dennis Williamson Jan 27 '11 at 22:25