-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename.sh
executable file
·84 lines (68 loc) · 2.4 KB
/
rename.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/bash
RENAME_BUFFER="/tmp/RENAME_BUFFER.toml"
echo "# Mass Rename Tool
# - Dont add new lines or delete existing ones
# - Rename with "DELETE" to delete the said files (macro @x)
# - Dont shuffle file order
"> "$RENAME_BUFFER" || {
echo -e "Failed to create $RENAME_BUFFER";
exit 1;
}
ORIGINAL_NAMES=()
if [ $# -eq 0 ]; then
IFS=$'\n'
for arg in $(ls); do
echo -e "$arg" >> "$RENAME_BUFFER"
ORIGINAL_NAMES+=("$arg")
done
unset IFS
else
for arg in "$@"; do
clean_arg="${arg#./}"
echo -e "$clean_arg" >> "$RENAME_BUFFER"
ORIGINAL_NAMES+=("$clean_arg")
done
fi
LINES_BEFORE=$(wc -l < "$RENAME_BUFFER" | tr -d ' ')
$EDITOR "$RENAME_BUFFER"
LINES_AFTER=$(wc -l < "$RENAME_BUFFER" | tr -d ' ')
if [ "$LINES_BEFORE" != "$LINES_AFTER" ]; then
echo -e "\033[31mError:\033[0m Name list mismatch"
rm "$RENAME_BUFFER"
exit 1
fi
for (( i=6; i<=$LINES_BEFORE; i++ )); do
original_name="${ORIGINAL_NAMES[$i-6]}"
new_name=$(awk "NR==$i" "$RENAME_BUFFER")
# do nothing if filename isn't changed
if [[ "$original_name" == "$new_name" ]]; then
continue
# delete file if new name is just DELETE
elif [ "$new_name" = "DELETE" ]; then
echo -e "\033[31mDeleting \033[36m$original_name"
rm -rf "./$original_name"
# if the file extensions are the same
elif [[ "${original_name##*.}" == "${new_name##*.}" ||
"$new_name" == "${new_name%.*}" ||
"$original_name" == "${original_name%.*}" ]];
then
echo -e "\033[33mRenaming \033[36m$original_name \033[33mto \033[0m$new_name"
mv "./$original_name" "./$new_name"
# check if ffmpeg is installed
elif ! command -v ffmpeg &> /dev/null; then
echo -e "\033[31mError:\033[0m Install ffmpeg to perform filetype conversions."
echo -e "\033[33mRenaming \033[36m$original_name \033[33mto \033[0m${new_name%.*}.${original_name##*.}"
mv "./$original_name" "./${new_name%.*}.${original_name##*.}"
# convert file
else
echo -e "\033[33mConverting \033[36m$original_name \033[33mto \033[0m$new_name"
ffmpeg -i "$original_name" "$new_name" -loglevel warning
# check if ffmpeg was successful
if [ $? -eq 0 ]; then
rm "$original_name"
else
echo -e "\033[31mError:\033[0m Conversion failed for $original_name"
fi
fi
done
rm "$RENAME_BUFFER"