-
Notifications
You must be signed in to change notification settings - Fork 0
/
renaming_resizing_script.sh
97 lines (80 loc) · 2.44 KB
/
renaming_resizing_script.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env bash
# Update this if there are changes to the arguments passed.
# shellcheck disable=SC2059
printf "Usage: \n* 'bash $0 dir_arg'\n* 'bash $0 percentage_arg dir_arg'\n* 'bash $0 width_arg height_arg dir_arg'\nAnything else will not work properly.\n"
execute_script() {
while [[ $prefix = '' ]];
do
printf '\nEnter chosen prefix: '
# shellcheck disable=SC2162
read prefix
done
# downloads/combined was the placeholder
# shellcheck disable=SC2016
printf "Target directory: $dir\n"
# shellcheck disable=SC2164
# shellcheck disable=SC2016
cd "$dir"
readonly LOG="chronicle.log"
now=$(date '+%d/%m/%Y | %r')
echo '-> New renaming started:' >> $LOG
# Declaring an array variable.
# shellcheck disable=SC2207
declare -a files=(`ls`)
printf 'Print read files in this folder (Y/n): '
# shellcheck disable=SC2162
read read_files
if [[ "$read_files" = 'Y' || "$read_files" = 'y' ]]
then
# shellcheck disable=SC2145
echo "Files here: ${files[@]}"
fi
printf 'Loader: \n'
echo '*'
# for loading the renaming and/or size conversion
loader=1
sp='/-\|'
echo -n ' '
id=0
for file in *.*
do
if [[ "$file" =~ \.(jpeg|png|jpg)$ ]]; then
id=$((id+1))
echo -ne "\b${sp:loader++%${#sp}:1}"
mv "$file" "$prefix-$id".jpg
if [[ "$percentage" != "" ]]; then
convert "$prefix-$id".jpg -resize "$percentage"% "$prefix-$id".jpg # standardize to a jpg
fi
if [[ "$width" != "" && "$height" != "" ]]; then
convert "$prefix-$id".jpg -resize "$width"x"$height" "$prefix-$id".jpg
fi
echo "$prefix-$id.jpg was $file" >> $LOG
fi
done
printf '\n*'
if [[ "$id" -ne 0 ]]; then
echo "-> Renaming files date: $now" >> $LOG
printf "\nRenaming complete. Check your renamed images in this file: $LOG\n"
else
printf '\nNo .jpeg, .png or .jpg files present in this dir.\n'
echo 'Aborted!' >> $LOG
fi
}
# echo "Arguments count: $#"
# Arguments passed.
if [[ "$#" -eq 1 ]]; then
dir="$1"
execute_script
elif [[ "$#" -eq 2 ]]; then
percentage="$1"
dir="$2"
execute_script
elif [[ "$#" -eq 3 ]]; then
width="$1"
height="$2"
# shellcheck disable=SC2034
dir="$3"
execute_script
else
printf 'Invalid argument size.\n'
fi