8

Cron installation is vixie-cron

/etc/cron.daily/rmspam.cron

#!/bin/bash
/usr/bin/rm /home/user/Maildir/.SPAM/cur/*;

I Have this simple bash script that I want to add to a cron job (also includes spam learning commands before) but this part always fails with "File or directory not found" From what I figure is the metachar isn't being interperted correctly when run as a cron job. If I execute the script from the commandline it works fine.

I'd like a why for this not working and of course a working solution :)

Thanks

edit #1 came back to this question when I got popular question badge for it. I first did this,

#!/bin/bash
find  /home/user/Maildir/.SPAM/cur/ -t file | xargs rm

and just recently was reading through the xargs man page and changed it to this

#!/bin/bash
find  /home/user/Maildir/.SPAM/cur/ -t file | xargs --no-run-if-empty rm

short xargs option is -r

2
  • Please edit and add the line from your crontab. Commented Oct 1, 2008 at 12:38
  • He mentioned in a comment below that it's in /etc/cron.daily Commented Oct 1, 2008 at 12:42

5 Answers 5

13

If there are no files in the directory, then the wildcard will not be expanded and will be passed to the command directly. There is no file called "*", and then the command fails with "File or directory not found." Try this instead:

if [ -f /home/user/Maildir/.SPAM/cur/* ]; then
    rm /home/user/Maildir/.SPAM/cur/*
fi

Or just use the "-f" flag to rm. The other problem with this command is what happens when there is too much spam for the maximum length of the command line. Something like this is probably better overall:

find /home/user/Maildir/.SPAM/cur -type f -exec rm '{}' +

If you have an old find that only execs rm one file at a time:

find /home/user/Maildir/.SPAM/cur -type f | xargs rm

That handles too many files as well as no files. Thanks to Charles Duffy for pointing out the + option to -exec in find.

Sign up to request clarification or add additional context in comments.

5 Comments

Actually, you don't need xargs with a new enough find: find /home/user/Maildir/.SPAM/cur -type f -exec rm -f '{}' '+'
shouldn't the find end with a \; ?
Deleting files is so frequent task that find also supports -delete action. No explicit need to do -exec rm '{}'.
It depends on your find. The BSDs and Linux have a -delete, but old-fashioned Unixes probably won't. Note that "old-fashioned" doesn't just mean "old", it can also mean "Solaris". My "find(1) muscle memory" comes from the 80s, where even '+' couldn't be used with exec, as seen in the first version of my answer; I had been using "find whatever -print0 | xargs -0 command" for a long time. Obviously at some point I adapted to spaces in filenames ...
-delete is not available in Ubuntu 16.04. Must be old.
0

Are you specifying the full path to the script in the cronjob?

00 3 * * * /home/me/myscript.sh

rather than

00 3 * * * myscript.sh

On another note, it's /bin/rm on all of the linux boxes I have access to. Have you double-checked that it really is /usr/bin/rm on your machine?

2 Comments

the script is located in /etc/cron.daily/ which is addressed in /etc/crontab
That is the default way that vixie-cron is setup on a gentoo distro. ya it is /bin/rm although there is a link to /usr/bin/rm. Maybe next time I'll use which rm to find the path instead of guessing
0

try adding

[email protected]

to the top of your cron file and you should get any input/errors mailed to you.

Also consider adding the command as a cronjob

0 30 * * * /usr/bin/rm /home/user/Maildir/.SPAM/cur/*

Comments

0

Try using a force option and forget about adding a path to rm command. I think it should not be needed...

rm -f

This will ensure that even if there are no files in the directory, rm command will not fail. If this is a part of a shell script, the * should work. It looks to me that you might have an empty dir...

I understand that the rest of the script is being executed, right?

Comments

0

Is rm really located in /usr/bin/ on your system? I have always thought that rm should reside in /bin/.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.