File: join-duplicates.sh

package info (click to toggle)
duff 0.5.2-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,188 kB
  • sloc: sh: 4,385; ansic: 3,132; makefile: 17; sed: 16
file content (44 lines) | stat: -rw-r--r-- 871 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/bin/sh
#
# Example script for duff(1).
#
# Copyright (c) 2005 Ross Newell
#
# Modified Jan 7, 2006 by Camilla Berglund <elmindreda@elmindreda.org>
#
# Uses duff to find duplicate physical files and changes them into hard links
# to a single physical file, thus saving disk space.  Use with care.
#

if [ "$1" == '' ]; then
  echo "usage: `basename $0` directory"
  exit 1
fi

duff -r '-f#' -z -p -P "$1" |
(
  while read file 
  do
    if [ "$file" == '#' ]; then
      first=''
    else
      if [ "$first" == '' ]; then
        first="$file"
      else
	temp=`mktemp -p \`dirname $file\``

	mv "$file" "$temp" && \
	ln "$first" "$file" && \
	touch --reference="$temp" "$file" && \
	rm "$temp"

	if [ $? != 0 ]; then
	  echo "`basename $0`: $file: failed to join with $first"
	  echo "`basename $0`: $file: may exist as $temp"
	  exit 1
	fi
      fi
    fi
  done
)