1 #!/bin/sh 2 # 3 # Example script for duff(1). 4 # 5 # Copyright (c) 2005 Ross Newell 6 # 7 # Modified Jan 7, 2006 by Camilla Berglund <elmindreda@elmindreda.org> 8 # 9 # Uses duff to find duplicate physical files and changes them into hard links 10 # to a single physical file, thus saving disk space. Use with care. 11 # 12 13 if [ "$1" == '' ]; then 14 echo "usage: `basename $0` directory" 15 exit 1 16 fi 17 18 duff -r '-f#' -z -p -P "$1" | 19 ( 20 while read file 21 do 22 if [ "$file" == '#' ]; then 23 first='' 24 else 25 if [ "$first" == '' ]; then 26 first="$file" 27 else 28 temp=`mktemp -p \`dirname $file\`` 29 30 mv "$file" "$temp" && \ 31 ln "$first" "$file" && \ 32 touch --reference="$temp" "$file" && \ 33 rm "$temp" 34 35 if [ $? != 0 ]; then 36 echo "`basename $0`: $file: failed to join with $first" 37 echo "`basename $0`: $file: may exist as $temp" 38 exit 1 39 fi 40 fi 41 fi 42 done 43 ) 44