I'm trying to find a way to check inside a given directory for duplicate files (even with different names) and replace them with symlinks pointing to the first occurrence. I've tried with
fdupes but it just lists those duplicates.
That's the context: I'm customizing an icon theme to my liking, and I've found that many icons, even if they have different names and different locations inside their parent folder, and are used for different purposes, basically are just the same picture. Since applying the same modification twenty or thirty times is redundant when just one is really necessary, I want to keep just one image and symlink all the others.
As an example, if I run
fdupes -r ./ inside the directory
testdir, it might return to me the following results:
./file1.png ./file2.png ./subdir1/anotherfile.png ./subdir1/subdir2/yetanotherfile.png
Given this output, I'd like to keep just the file
file1.png, delete all the others and replace them with symlinks pointing to it, while maintaining all original file names. So
file2.png will retain its name, but will become a link to
file1.png instead of being a duplicate.
Those links should not point to an absolute path, but should be relative to the parent
testdir directory; i.e.
yetanotherfile.png will be point to
../../file1.png, not to
I'm interested both in solutions that involve a GUI and CLI. It is not mandatory to use
fdupes I've cited it because it is a tool that I know, but I'm open to solutions that use other tools as well.
I'm pretty sure that a bash script to handle all of this should not be that difficult to create, but I'm not expert enough to find out how to write it myself.