Using rsync to only delete extraneous files
Solution 1
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.
Solution 2
You need --existing
too:
rsync -r --delete --existing --ignore-existing /home/gallery/images /home/gallery/thumbs
From the manpage:
--existing, --ignore-non-existing This tells rsync to skip creating files (including directories) that do not exist yet on the destination. If this option is combined with the --ignore-existing option, no files will be updated (which can be useful if all you want to do is delete extraneous files).
Related videos on Youtube
Kcmamu
Developer, Systems Admin, Geek, Gadget lover, etc. etc. I started programming in BASIC at the age of 11 on a Sinclair ZX81, advanced to a BBC Model B, where I learned 6502 assembly language programming. I never really worked with PCs until the early 90s. In the late 90s, I joined a higher educational institution as a desktop technician, a quickly got promoted to be a systems admin, working predominantly on Windows systems, but also had a keen interest in Linux systems. I later got involved in software development, working in C#, PHP, C. In my current employment, I'm the manager of the company's Information Systems department. The primary focus of our business is industrial control systems (mostly legacy systems). The work isn't exclusively legacy/control systems though, as we also support modern systems for a number of business customers.
Updated on September 18, 2022Comments
-
Kcmamu over 1 year
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?
-
Kcmamu about 13 yearsThanks 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
? -
Naman Bansal about 13 years@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 withecho
! -
Kcmamu about 13 yearsMany thanks, I'll apply copious amounts of echo whilst testing.
-
Naman Bansal about 13 yearsJust had a thought: you could test with "ls" as well to ensure it works well with whitespace. Best wishes.
-
Mecki over 4 years@LonnieBest If there are errors, not even
rm
,cp
ormv
will work as that's what errors are: They are problems that should be looked at and that prevent operations from completing successful. You can instruct most tools to ignore errors (e.g.-f
forrm
) but I don't see how that's relevant to the question or this answer. -
user2284570 about 3 years@Joril it did nothing on my end.