Using find,sed and xargs to strip files from one folder that exist in another

Handy bash commands to remove copies of files that exist in two places when you only want unique files in each folder:

Scenario:
Folder A contains 20000 files
Folder B contains 50000 fies

… and you have accidentally copied all of folder A into folder B so you now have thousands of duplicate files in folder B.

You can easily strip out any files in Folder B from Folder A using 3 bash commands piped together like so:

find foldera/* | sed s@foldera/@folderb/@ | xargs rm -rf

  • find – locate all files in folder a (you can use any combination of find paramaters here of course)
  • sed – replace foldera with folder b in each line of the results
  • xargs – run the rm -rf command on the output from sed
This just save me a lot of  time. Hope it helps someone else.
Credits to CiaranG for the assist with piping find results into sed, nice work!