#!/bin/sh - # # make-hardlinks # # Find files with identical contents (MD5-based) and save space by # re-creating them as hardlinks. Tested and works on FreeBSD. # # Copyright (C) 2005-2009 Oliver Fromme # All rights reserved. Standard 2-clause BSD license and disclaimer applies. # for i in "$@"; do if [ ! -d "$i" ]; then echo "${0##*/}: ${i}: Not a directory." >&2 echo "Usage: ${0##*/} [ ...]" >&2 echo "If no directory is specified, start scanning in the current directory." >&2 exit 2 fi done if [ $# -lt 1 ]; then set . fi find -- "$@" -type f -not -empty -print0 | xargs -0 md5 -r \ | awk ' BEGIN { a = "'\''" n = 0 } { if (h[$1]) { x1 = h[$1] x2 = $2 gsub(/'\''/, a "\\" a a, x1) gsub(/'\''/, a "\\" a a, x2) cmd[n] = sprintf("ln -f '\''%s'\'' '\''%s'\''", x1, x2) n++ } else h[$1] = $2 } END { k = 0 for (i = 0; i < n; i++) if (system(cmd[i]) == 0) k++ else { print "System command failed:" > "/dev/stderr" print cmd[i] > "/dev/stderr" } print k, "links created." > "/dev/stderr" if (k < n) { print n - k, "ERRORS." > "/dev/stderr" exit 1 } } ' #--