Decades ago, I remember coming across a Perl script, probably in Larry Wall’s book, for finding identical files and linking them. Thanks mainly to Moore’s Law, I never actually had to follow that advice for saving space until yesterday, on one of my ancient VPSs:
#!/bin/bash
for file in $(find . -type f); do
echo checking $file...
if diff --brief $file ${file##.}; then
echo ${file##.} >> same.txt # diff returns 0 (true) if same
ls --inode ${file##.} $file
# check inodes before and after
ln --force ${file##.} $file # make this a copy of system file
ls --inode ${file##.} $file # check inodes before and after
#exit # exit on first file while testing, then comment out
else
echo ${file##.} >> different.txt
fi
done
After the links were created, I did a little testing to see how they actually work. Does deleting one erase the other? Nope. Does editing one create a new inode for the edited file, leaving the other intact? Again, no, at least for a small edit that doesn’t change the file’s allocated size; the inode stays the same, so the contents are altered in both locations. This makes it useless as a backup copy, but as long as I can remember that, it’s fine; my disk usage went from 87% to 77%.