Search code examples
linuxdiffunlink

Delete identical files


I'm able to find all identical files in 2 directories with the following command:

diff -srq sub1/ sub2/ | grep identical

Is it possible to delete founds one easily, so I only have the not identical files in both directories?


Solution

  • This is off-topic on this site, but I'll answer. Since your question is somewhat unclear to me, whether you want to delete all identical files, or only one of the files, so I'll give you both options.

    This deletes all matched files:

    diff -srq sub1/ sub2/ | grep identical | xargs rm
    

    This deletes only first matched file:

    diff -srq sub1/ sub2/ | grep identical | head -1 | xargs rm
    

    Note, I haven't tested this, but at least you have a starting point.