-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoin-duplicates.sh
45 lines (40 loc) · 926 Bytes
/
join-duplicates.sh
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 "Snow"
#
# Modified Jan 7, 2006 by Camilla Berglund <[email protected]>
#
# 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 -t "$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" && \
echo "Removed duplicate of $first: $file"
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
)