-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindspace-confirm.sh
62 lines (49 loc) · 1.93 KB
/
findspace-confirm.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
#!/bin/bash
# If no argument is provided, use the default directory
if [ -z "$1" ]; then
search_directory="/Users/Shared/"
else
search_directory="$1"
fi
# Log file name with current date and time
log_file="corrections_$(date +"%Y%m%d_%H%M%S").log"
log_file_path="$search_directory/$log_file"
# Initialize corrections counter
corrections=0
# Find directories with spaces and store the results in a temporary file
tmp_file=$(mktemp)
# Find command with depth, type, and name options to search for directories with spaces
find_cmd=(find "$search_directory" -depth -type d -name '* ' -print0)
# Enable extended globbing for pattern matching
shopt -s extglob
# Execute the find command and store the results in the temporary file
"${find_cmd[@]}" | tr '\n' '\0' > "$tmp_file"
# If no directories with trailing spaces are found, exit with a message
if [ ! -s "$tmp_file" ]; then
echo "No directories with trailing spaces found. No changes needed."
rm "$tmp_file" # Remove the temporary file
exit 0
fi
# Display found directories and ask for confirmation before proceeding with changes
echo "The following directories have trailing spaces:"
while IFS= read -r -d '' dir_with_space; do
echo "$dir_with_space"
done < "$tmp_file"
# Ask for confirmation to proceed with changes
read -p "Do you want to proceed with removing the trailing spaces? (y/n): " answer
# If the answer is yes, proceed with corrections
if [ "$answer" = "y" ]; then
while IFS= read -r -d '' source_name; do
dest_name="${source_name%"${source_name##*[![:space:]]}"}"
mv_log="$(date +"%Y-%m-%d %H:%M:%S") - Moved '$source_name' to '$dest_name'"
mv -- "$source_name" "$dest_name" && echo "$mv_log" >> "$log_file_path"
((corrections++))
done < "$tmp_file"
# Display the number of corrections made
echo "Corrections made: $corrections"
# Remove the temporary file
rm "$tmp_file"
else
echo "No changes were made."
rm "$tmp_file" # Remove the temporary file
fi