Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

The following script sometimes gives me things like can't remove a/b as it's not empty

ssh -T user@host <<EOF
  cd somewhere
  rm -rf a/b    
EOF

But I log in to the server and execute rm -rf a/b then I never had the problem.

There are other processes that are generating files to a/b, is that related?

Anyway, how to make a script that will ensure "a/b" will be deleted?

share|improve this question
1  
That's funny, rm -rf should always succeed, short of permission issues. Ask the script to print debugging output if the removal fails.ls -laR a/b should suffice. –  Faheem Mitha May 4 '11 at 16:41
add comment

2 Answers

up vote 3 down vote accepted

rmdir(2) will fail if the directory is not empty. If another process is creating files while rm(1) is removing them, it will not know to delete them and consequently when it comes time for rm(1) to try to delete what it believes should be an empty directory, it will fail with the error you've posted.

One way to delete the directory in the face of concurrent file creations in the directory is to rename it:

mv a a~
rm -rf a~

It's possible that this may not work if the processes creating the files in a/b are not doing so by path (open(2) vs. openat(2)).

I am assuming that the process(es) that creates files in a/b will recreate that directory if it does not exist, or will handle failure gracefully if it does not exist. Since you are already trying to delete the directory from under other processes, that seems like a safe assumption.

share|improve this answer
add comment

There are other processes that are generating files to a/b, is that related?

Very possibly. It might be that rm -rf first removes all files, then removes all directories. Under the hood, the rmdir system call for removing a directory will fail if the directory is not empty. You might have a race condition going on, where rm -rf checks that the directory is empty, and it is; another process then creates a new file; finally rm -rf calls rmdir, but another process creates a file beforehand.

Faheem's advice is good: put an ls into your error condition

ssh -T user@host <<EOF
  cd somewhere
  rm -rf a/b || ( ls -a a/b && exit 1 )
EOF
share|improve this answer
    
Very interesting. –  boehj May 5 '11 at 4:01
    
The && should be ; or else ls failure will prevent the script from exiting. –  R.. May 7 '11 at 2:06
    
Yep. ls shows that there are new files in that dir. I'm going to give the mv solution a try. –  Golf November May 7 '11 at 13:08
    
@R, that is not true. If ls fails, the subshell will return ls's failure code. the '&& exit 1' is necessary to ensure that if ls succeeds, the sub-shell still returns an error code. If you substitute it for '||', you will mask the true error code for ls. –  jmtd May 9 '11 at 13:44
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.