Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

When running this command:

$ sudo rsync -r --delete --force --checksum --exclude=uploads /data/prep/* /data/app/

I'm getting the following output:

cannot delete non-empty directory: html/js/ckeditor/_source/plugins/uicolor/yui
cannot delete non-empty directory: html/js/ckeditor/_source/plugins/uicolor/yui
cannot delete non-empty directory: html/js/ckeditor/_source/plugins/uicolor
cannot delete non-empty directory: html/js/ckeditor/_source/plugins/uicolor
cannot delete non-empty directory: html/js/ckeditor/_source/plugins
cannot delete non-empty directory: html/js/ckeditor/_source/plugins
cannot delete non-empty directory: html/js/ckeditor/_source
cannot delete non-empty directory: html/js/ckeditor/_samples
cannot delete non-empty directory: html/js/ckeditor/plugins/uicolor/yui
cannot delete non-empty directory: html/js/ckeditor/plugins/uicolor/yui
cannot delete non-empty directory: html/js/ckeditor/plugins/uicolor

From reading the man rsync it was my impression that the --force option would tell rsync to delete these non-empty directories, which is the desired result.

Ref:

--force                 force deletion of dirs even if not empty

How can I modify the command to delete the non-empty directories?

I'm using rsync version 3.0.8, on Gentoo Base System release 2.0.3, in case that's relevant.

Update: Added sudo to the command to make it clear that this is not a file permissions issue.

share|improve this question
    
What filesystem is the destination? Is the filesystem in need of repair? –  Marcus Downing Feb 21 at 9:40
    
The file system is ext3. It's possible the filesystem could be in need or repair. I'll fsck at the next opportunity and update with results. –  tommarshall Feb 21 at 10:24
    
I've just fsck'd the system and this issue is still present. –  tommarshall Feb 21 at 11:30

3 Answers 3

up vote 5 down vote accepted

Did you try to add --delete-excluded?

Message cannot delete non-empty directory happens easily if delete a directory on the source side and then you want to run rsync --delete. Rsync will not let you delete the excluded files in the directory.

share|improve this answer
    
There was a folder called uploads in the html/js/ckeditor/_source/plugins/uicolor/yui, html/js/ckeditor/_samples and html/js/ckeditor/plugins/uicolor/yui directories. Thanks for your help. –  tommarshall Jul 15 at 8:46

A directory has to be empty in order for you to delete it, the file system normally requires that.

Therefore normally rsync or rm would recursively delete all the contents first and only then delete the now-empty directory.

If the current user is not the owner of all files, the file systems permissions won't allow you to delete those files. Since they won't be removed, the directory isn't emptied and the delete will fail.

My first guess would be that some files in those directory are owned by another user e.g. the apache or nobody users.

share|improve this answer
    
Thanks. The files within the directories are indeed owned by a user within the apache group, however I've just attempted to run this command as that user and got the same errors. –  tommarshall Feb 5 at 16:36
    
Also, if it was a file permissions issue I'd expect running the same command as with 'sudo' to resolve the issue, but this is not the case. However, please correct me on this if I am mistaken. –  tommarshall Feb 5 at 16:38
1  
Running the command as root should overcome most permission issues, yes. (likely place where that would still fail is on a nfs mount to name one, an immutable file another) A simple check with ls -la to see why the directory is still not empty. lsattr will display immutable files. –  HBruijn Feb 5 at 19:49
    
Thanks for the additional info, however lsattr from one of the directories listed outputs the following: --------------- ./assets (no i flag), and the file is not on a nfs mount. Are there any other reasons you could think of why the command would still fail with sudo? –  tommarshall Feb 6 at 9:31
    
Are there any symlinks in there, or just real files? –  Marcus Downing Feb 21 at 9:34

Here are possible sources of this problem:

(1) This error can be the result of the -b (--backup) option. This option will create a backup of each deleted file, by appending a tilde (~) on its file name. (This confused me, as the file-name is clearly a backup, but the directory name is not, as you can't see the tilde.)

To check if this is the same case, read your target directory on the deepest level, and check if there is any tilde (~) ending file. Note that these tilde-appended-files-names are then invisible in some common file browsing system, so you may not see them.

To solve this case, prefer the --backup-dir=DIR option, for instance --backup-dir=.rsync_bak.

(2) The --exclude option can have the same results. Which is possibly happening in your case. The pattern system is powerful, but may be misleading. For instance if you write --exclude='*~', this will skip all tilde ending files, resulting exactly like in case (1) above.

from rsync man page:

if the pattern starts with a / then it is anchored to a particular spot in the hierarchy of files, otherwise it is matched against the end of the pathname

Si if you write --exclude=uploads, this will exclude all files named "updloads", at any level of your files tree.

Check if there is a file named "uploads" inside your unable-to-delete directories.

Solution would be to change "--exclude=uploads" to "--exclude=uploads/"

share|improve this answer

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.