Breaking (unlinking) hard links

December 19, 2016

Sometimes, you have so much data that you need to look at de-duplicating it. One method for this is to use hard links.

What happens if you mess up and need to remove those hard links though?

I recently created this script which will find all files in a given directory with more than one link to it. It will then break (unlink) the hard link by copying and subsequently moving back the original file. You can then re-create your hard links (or try something else) as necessary!

#!/usr/bin/env bash

find /path/to/directory -type f -links +1 | while read link; do
    original="$link"
    tmp="$link.$$"
    links=$(stat -c '%h' "$original")

    echo -n "Unlinking $original ($links links)... "
    cp -p "$original" "$tmp"
    mv -f "$tmp" "$original"
    echo 'done'
done
comments powered by Disqus