I am trying to compare 2 directories ( A / B ) in Linux and DELETE any files from B which DO NOT exist in A.
For example, if file '1.jpg' exists in Directory B but does not exist in Directory A then it needs to be deleted from B.
I have tried using diff but all the files are essentially different so it doesn't seem to work. (They are thumbnails of different sizes but have the same id's). So, this has to be done by file name only and ignore the actual contents of the file.
Can someone please shed some light on how to do this with minimal effort?
Best Answer
rsync
can do what you want quickly and easily:From the help:
Remove the
dry-run
option after you're satisfied with the proposed results, to actually execute the deletions.The man page has more explicit description of the options and even mentions your use case: