Olivier Pons
Olivier Pons

Reputation: 15778

linux shell find, compare and delete files

I have two directories, and in one of them I renamed a lot and now I have more than 2000 different files.

I'd like to find out all the files of the same size, do a CRC on files with identical sizes, and if they match, delete file from the second folder.

After googling, I found BeyondCompare and other nice diff utilities, but they dont do this.

I'm pretty sure a one-liner in a Linux shell could do this.

Upvotes: 1

Views: 219

Answers (2)

Gilles Quénot
Gilles Quénot

Reputation: 185005

You can use a dedicated tool, fdupes

$ fdupes -rfd dir1 dir2

Upvotes: 3

HappyCactus
HappyCactus

Reputation: 2014

Use an MD5 sum:

$ md5sum firstdir/* | sort > first.txt
$ md5sum secondidr/* | sort > second.txt
$ comm -12 first.txt second.txt | xargs rm

It is not a one-line command, but it shouldn't be difficult to combine these commands.

Upvotes: 0

Related Questions