bublina

Love people, use things.

Playing with hard links

Hard links can be very useful, but also nasty. I used them to my advantage when I was hosting the same files on multiple domains, but they bit me back because Git does not support them.

First, how to make hard links if there are the same files with same names in the same directory structure?

#!/bin/sh

MDIR=${1:-"$HOME/some-default"}
MDIR=${MDIR%%/}

find $MDIR -type f \
  | sed "s|^$MDIR/||" \
  | while read f; do cmp $f $MDIR/$f 2>/dev/null \
    && ln -vnf $MDIR/$f $f; done

And in order to revert it, following script might be useful:

#!/bin/sh

find . -type f -exec ls -l {} \; \
  | awk '{if ($2 >= 2) {print $9;}}' \
  | while read file
    do
      cp "$file" "$file-real" \
        && rm "$file" \
        && mv -v "$file-real" "$file"
    done