-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfind_duplicates.sh
58 lines (48 loc) · 1.62 KB
/
find_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
#!/bin/bash
# Javier Ferrándiz Fernández - 31/07/2023 - https://github.com/javisys
dir="$HOME"
# Create a temporary file for storing the list of files and their hashes
tmp_file="/tmp/duplicate_files.tmp"
# Function to calculate the MD5 hash of a file
calculate_md5() {
md5sum "$1" | awk '{print $1}'
}
# Loop to generate the list of files and their hashes
find "$dir" -type f -print0 | while IFS= read -r -d '' file; do
calculate_md5 "$file" >> "$tmp_file"
echo "$file" >> "$tmp_file"
done
# Identify duplicate files
duplicates=$(sort "$tmp_file" | uniq -d --all-repeated=separate | awk 'NR % 2 == 0')
# Show duplicate files and allow user to choose what to do
if [ -n "$duplicates" ];
then
echo "Duplicate files have been found:"
echo "$duplicates"
echo
echo "What do you want to do with duplicate files?"
echo "1.- Delete all duplicates"
echo "2.- Keep only one file from each set of duplicates"
echo "3.- Do nothing (leave without changes)"
read -p "Select an option: " option
case "$option" in
1)
echo "$duplicates" | xargs rm
echo "Duplicate files deleted."
;;
2)
echo "$duplicates" | uniq -u --all-repeated=separate | xargs rm
echo "Duplicate files deleted, only one file from each set was retained."
;;
3)
echo "No changes were made. Duplicate files are maintained."
;;
*)
echo "Invalid option. No changes were made."
;;
esac
else
echo "No duplicate files were found in the specified directory"
fi
# Delete temporary file
rm "$tmp_file"