-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathjoin-duplicates.sh
47 lines (42 loc) · 919 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
45
46
#!/bin/bash
#
# Example script for duff(1).
#
# Copyright (c) 2005 Ross Newell
#
# Modified Sep 7, 2012 by Camilla Löwy <dreda@dreda>
#
# 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 [ $# == 0 ]; then
echo "Usage: `basename $0` directory [...]"
exit 1
fi
duff -0Dprz -f '%n' -- "$@" |
(
count=0
while IFS='' read -d '' -r line
do
if [ "$count" == 0 ]; then
count="$line"
first=''
else
if [ "$first" == '' ]; then
first="$line"
else
file="$line"
temp="`mktemp -p \`dirname $file\``"
mv "$file" "$temp" && \
ln "$first" "$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
count="`expr $count - 1`"
fi
done
)