-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileNameTidy.sh
65 lines (56 loc) · 1.61 KB
/
FileNameTidy.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
#!/bin/bash
# Print script usage instructions
usage() {
echo "Usage: $(basename $0) [-y]"
echo "Renaming video files in the current directory."
echo " -y Assume yes to all confirmations"
exit 1
}
# Check if a confirmation is required
get_confirmation() {
if [ "$assume_yes" = true ]; then
return 0
fi
while true; do
read -rp "Rename this file '$1' to '$2'? [Y/N]: " answer
case $answer in
[Yy]* ) return 0;;
[Nn]* ) return 1;;
* ) echo "Please answer yes or no.";;
esac
done
}
# Process each video file in the current directory
rename_video_files() {
for file in *.mp4 *.avi *.m4v *.wmv, *.mkv; do
# Skip if no files found
[ -e "$file" ] || continue
# Extract file name and extension
filename=$(basename -- "$file")
extension="${filename##*.}"
filename="${filename%.*}"
# Remove year and resolution from filename
new_filename=$(echo "$filename" | sed -E 's/([a-zA-Z0-9]+)[._](20[0-9]{2}|1080p?).*/\1/g' | tr '.' ' ')
# Confirm renaming of file
if get_confirmation "$filename.$extension" "$new_filename.$extension"; then
# Rename file
mv "$filename.$extension" "$new_filename.$extension"
echo "Renamed '$filename.$extension' to '$new_filename.$extension'"
else
echo "Skipping '$filename.$extension'"
fi
done
}
# Parse command-line arguments
assume_yes=false
while getopts "hy" option; do
case $option in
h ) usage;;
y ) assume_yes=true;;
* ) usage;;
esac
done
# Rename video files in the current directory
echo "Renaming video files in the current directory:"
rename_video_files
echo "Done!"