Skip to content

Commit 168b3c3

Browse files
committed
Added duplicate_clean to check for similar titles
1 parent 8760f2a commit 168b3c3

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

duplicate_clean.sh

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/bin/bash
2+
3+
# Check if directory is passed as an argument
4+
if [ -z "$1" ]; then
5+
echo "Usage: $0 <directory>"
6+
exit 1
7+
fi
8+
9+
DIRECTORY="$1"
10+
11+
# Check if the directory exists
12+
if [ ! -d "$DIRECTORY" ]; then
13+
echo "Error: Directory '$DIRECTORY' does not exist."
14+
exit 1
15+
fi
16+
17+
# Find all files in the directory
18+
files=$(find "$DIRECTORY" -type f)
19+
if [ -z "$files" ]; then
20+
echo "No files found in the directory."
21+
exit 0
22+
fi
23+
24+
# Group similar files by fuzzy matching prefixes
25+
declare -A file_groups
26+
while IFS= read -r file; do
27+
base_name=$(basename "$file" | sed 's/\.[^.]*$//')
28+
matched=false
29+
30+
for key in "${!file_groups[@]}"; do
31+
if [[ "$base_name" == *"$key"* || "$key" == *"$base_name"* ]]; then
32+
file_groups["$key"]+="$file\n"
33+
matched=true
34+
break
35+
fi
36+
done
37+
38+
if [ "$matched" = false ]; then
39+
file_groups["$base_name"]="$file\n"
40+
fi
41+
done <<< "$files"
42+
43+
# Process groups with more than one file
44+
found_similar=false
45+
for base_name in "${!file_groups[@]}"; do
46+
group_files=$(echo -e "${file_groups[$base_name]}")
47+
file_count=$(echo "$group_files" | wc -l)
48+
49+
if (( file_count > 1 )); then
50+
found_similar=true
51+
echo "Similar files found for base name: $base_name"
52+
53+
# Display group files with numeric options
54+
count=1
55+
declare -a file_list
56+
file_list=() # Reset file_list for each group
57+
while IFS= read -r file; do
58+
echo "[$count] $file"
59+
file_list+=("$file")
60+
((count++))
61+
done <<< "$group_files"
62+
63+
echo ""
64+
echo "Enter the numbers of the files you want to delete (separate by space), or press Enter to keep all:" >&2
65+
read -rp "Your choice: " delete_choices
66+
67+
if [ -n "$delete_choices" ]; then
68+
for choice in $delete_choices; do
69+
if [[ $choice =~ ^[0-9]+$ ]] && ((choice > 0 && choice <= ${#file_list[@]})); then
70+
file_to_delete="${file_list[$((choice - 1))]}"
71+
echo "Attempting to delete: \"$file_to_delete\""
72+
rm -f "$file_to_delete" 2>/dev/null
73+
if [ $? -eq 0 ]; then
74+
echo "Deleted: \"$file_to_delete\""
75+
else
76+
echo "Failed to delete: \"$file_to_delete\""
77+
fi
78+
else
79+
echo "Invalid selection: $choice"
80+
fi
81+
done
82+
else
83+
echo "Keeping all files in this group."
84+
fi
85+
echo ""
86+
fi
87+
done
88+
89+
if ! $found_similar; then
90+
echo "No similar file names found in the directory."
91+
fi
92+
93+
echo "No more duplicates remain!"

0 commit comments

Comments
 (0)