Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
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
add comment

4 Answers

up vote 1 down vote accepted

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.

share|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
add comment

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

#!/bin/bash
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.

share|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. –  Alexander Burke Mar 18 '10 at 4:44
add comment

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

share|improve this answer
add comment

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

share|improve this answer
add comment

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.