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.

What's the best way of comparing two directory structures and deleting extraneous files and directories in the target location?

I have a small web photo gallery app that I'm developing. Users add & remove images using FTP. The web gallery software I've written creates new thumbnails on the fly, but it doesn't deal with deletions. What I would like to do, is schedule a command/bash script to take care of this at predefined intervals.

Original images are stored in /home/gallery/images/ and are organised in albums, using subdirectories. The thumbnails are cached in /home/gallery/thumbs/, using the same directory structure and filenames as the images directory.

I've tried using the following to achieve this:

rsync  -r --delete --ignore-existing /home/gallery/images /home/gallery/thumbs

which would work fine if all the thumbnails have already been cached, but there is no guarantee that this would be the case, when this happens, the thumb directory has original full size images copied to it.

How can I best achieve what I'm trying to do?

share|improve this question

1 Answer 1

up vote 6 down vote accepted

I don't think rsync is the best approach for this. I would use a bash one-liner like the following:

$ cd /home/gallery/thumbs && find . -type f | while read file;do if [ ! -f "../images/$file" ];then echo "$file";fi;done

If this one-liner produces the right list of files, you can then modify it to run an rm command instead of an echo command.

share|improve this answer
    
Thanks Tom. I guess to also clean the directories, I'd need to run it a second time, but specifying directories in the commands instead of files, and substituting the echo with rmdir? –  Bryan May 31 '11 at 13:42
    
@Bryan: Yes that sounds reasonable. You'd need to change the flags in the find and in the [ test ]. Of course, please be very careful both with the command I've given you and any modifications, and test thoroughly with echo! –  Tom Shaw May 31 '11 at 13:49
    
Many thanks, I'll apply copious amounts of echo whilst testing. –  Bryan May 31 '11 at 14:07
1  
Just had a thought: you could test with "ls" as well to ensure it works well with whitespace. Best wishes. –  Tom Shaw May 31 '11 at 14:11
    
Good idea. Thanks again. –  Bryan May 31 '11 at 19:28

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.