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?

  • I did exactly as Lee suggested (rm -rf) instead of just rm and that did the trick for me. – user113544 Jan 13 '12 at 19:06

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.

| improve this answer | |
  • Spaces in the path of the files (or directories) will be automatically handled by the pathname expansion mechanism of the shell. – mrucci Mar 18 '10 at 7:20
  • ...mark your own answer as accepted answer. This will make it easy for other user to notice that you have found a solution. – mrucci Mar 18 '10 at 7:38
  • Actually I already tried to do that but apparently this site has a 7 hour wait period before you can accept your own answer. – Nicholas Mar 18 '10 at 11:31

Try placing the following in a script and calling it from your cron job.

rm -rf /full/path/to/dir/*

The cron job is probably failing as your using rm without the -f (force) option. rm will ask everytime if you are sure that you want to delete a file.

The added -r (recursive) option will ensure all directories under /full/path/to/dir/ are also deleted.

The rm man page can be found here.

| improve this answer | |
  • Using the '-rf' flags return the same negative result. I'm also already using that bash shebang. – Nicholas Mar 16 '10 at 11:56
  • Can you do a ls on a file which you know exists? I'm thinking that possibly you've got 'set -f', which disables glob expansion. – gorilla Mar 16 '10 at 12:18
  • I've tried placing 'set +f' inside the script but there is no change in the result. – Nicholas Mar 17 '10 at 10:34
  • Try set +x instead. Also, rm -f only asks you every time because most distributions have rm aliased to rm -i for root. OP already stated there is no alias. – Alex Mar 18 '10 at 4:44

You didn't say what operating system you were on, but could SELinux be interfering?

| improve this answer | |

What do you get in you use rm -v /full/path/to/directory/*, and what's does that glob expand to? Perhaps there are too many arguments - if so, use something like find /full/path/to/directory -delete; mkdir -p /full/path/to/directory

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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