-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-hardlinks.sh
More file actions
executable file
·82 lines (66 loc) · 1.65 KB
/
create-hardlinks.sh
File metadata and controls
executable file
·82 lines (66 loc) · 1.65 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env bash
##################################################################################################################
## Check in a directory (or in a source and in a target directory) duplicates and affect them to the same inode ##
##################################################################################################################
if [[ "" == "$1" ]]
then
echo "usage: ${BASH_SOURCE[0]} path1 [path2]"
exit 1
fi
path1="$1"
if [[ "" == "$2" ]]
then
path2="$path1"
else
path2="$2"
fi
if [[ ! -d "$path1" ]]
then
echo "ERROR: $path1 doesn't exist"
exit 1
fi
if [[ ! -d "$path2" ]]
then
echo "ERROR: $path2 doesn't exist"
exit 1
fi
if [[ "$(uname)" == "Darwin" ]] # Mac OSX
then
shasum="shasum"
else
shasum="sha1sum"
fi
echo "Looking in $path2 duplicates of files from $path1"
start=$(date +%H:%M:%S)
echo "Start $start"
startTms=$(date +"%s")
find "$path1" -type f | while read f
do
filename=$(echo "$f" | sed -E 's/.*\/([^\/]*)$/\1/')
if [[ "." == ${filename:0:1} ]]
then
continue
fi
find "$path2" -name "$filename" | while read f2
do
inode1=$(ls -i "$f" | cut -d ' ' -f 1)
inode2=$(ls -i "$f2" | cut -d ' ' -f 1)
if [[ "$inode1" == "$inode2" ]]
then
continue # Same file or already an hard link
fi
shasum1=$("$shasum" "$f" | cut -d ' ' -f 1)
shasum2=$("$shasum" "$f2" | cut -d ' ' -f 1)
if [[ "$shasum1" != "$shasum2" ]]
then
continue # Another different file which has the same name
fi
echo -n "Creating hardlink from $f2 to $f ... "
ln -f "$f2" "$f"
echo "done"
done
done
endTms=$(date +"%s")
end=$(date +%H:%M:%S)
echo "End $end"
exit 0