-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete-media-duplicates.sh
executable file
·66 lines (58 loc) · 1.53 KB
/
delete-media-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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/bash
files_dir="$1"
if [[ -z '$files_dir' ]]; then
echo "Error: files dir is undefined"
exit;
fi
#generate md5 hashes
md5array=()
while read folder;
do
echo "$folder"
count=0
while read file
do
filename=$(basename "$file")
echo ">>> $filename"
md5=$(ffmpeg -loglevel quiet -v quiet -err_detect ignore_err -i "$file" -f md5 - < /dev/null )
#printf "\e[91m%s:%s\n" "$md5" "$file"
md5array+=( "$md5:$file")
let "count++"
done < <(find "$folder" -type f)
printf "Analized $count Songs in $folder\n\n"
done < <(find $files_dir -type d -links 2)
printf "\n%s\n" "Found ${#md5array[@]} Songs\n"
#printf "%s\n" "${md5array[@]}"
#sort by md5
readarray -t sortedmd5 < <(for e in "${md5array[@]}"; do echo "$e"; done | sort)
#printf "%s\n" "${sortedmd5[@]}"
#split into md5 and fullpath
md5=()
fullpath=()
IFS=''
for song in "${sortedmd5[@]}"
do
md5+=($( echo "$song" | cut -f1 -d":" ))
fullpath+=($( echo "$song" | cut -f2 -d":" ))
done
#printf "%s\n" "${md5[@]}"
#printf "%s\n" "${fullpath[@]}"
#detect dupes
countdupes=0
dupes=()
for ((idx=0; idx<${#md5[@]}-1; ++idx)); do
if [[ ${md5[$idx]} = ${md5[$idx+1]} ]]
then
let "countdupes++"
dupes+=( "${fullpath[$idx]}" )
echo "${idx}" "${md5[idx]}" "${fullpath[$idx]}"
fi
done
printf "Found %s duplicates\n" "$countdupes"
#printf "%s\n" "${dupes[@]}"
for f in "${dupes[@]}"
do
echo "rm $f" #remove echo to delete
done
printf "Deleted %s files in %s" "${#dupes[@]}" "$files_dir"
echo