Find missing file names between 2 directories in Linux

diff()filenamesrmsynchronization

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:

rsync --dry-run --verbose --recursive --existing --ignore-existing --delete-after A/ B/

From the help:

 --existing              skip creating new files on receiver
 --ignore-existing       skip updating files that already exist on receiver
 --delete                delete extraneous files from destination dirs

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:

   --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 to  delete
      extraneous files).


   --ignore-existing
      This  tells  rsync  to skip updating files that already exist on
      the destination (this does not ignore  existing  directores,  or
      nothing would get done).  See also --existing.