Rm failing inside cron script

cron

I have a cron job calling a bash script which runs fine, except for one line inside it that is suppose to remove all fines in a directory. The result of this line is always 'no such file or directory' even though I have verified (many times) that there are files in that directory. The line in question is as simply:

rm /dir1/dir2/dir3/*

The script works fine when run manually in the terminal, so it must be something about how the cron is run. I've tried giving 'dir3' and all the files inside it every permission possible, so it shouldn't be a permission problem. (The directory and files are also owned by the user). I've tried specifing 'SHELL=/bin/bash' inside 'crontab'. There is no sticky bit set and there is no alias on the rm command.

Interestingly changing the 'rm' command to 'ls' gives the same negative result (unless you remove the trailing '*', and then that works).

What am I missing here?

Best Answer

Turned out to be my bad after all. What I actually had in the script was:

rm "/dir1/dir2/dir3/*"

I didn't realise that the quotes would be blocking the wildcard expansion so I never included them in the question. Removing the quotes fixed the problem, as I'm sure would have been pointed out to me if I'd included them. Although I'm not 100% sure how one is supposed to handle spaces in the path without the quotes thought.

Related Question