-
Notifications
You must be signed in to change notification settings - Fork 3
/
zero-pad.sh
executable file
·49 lines (38 loc) · 1.27 KB
/
zero-pad.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
#!/bin/bash
# Set the directory path
directory_path="/home/young/Desktop/GUSTO-DATA"
# Check if the directory exists
if [ ! -d "$directory_path" ]; then
echo "Error: Directory not found."
exit 1
fi
# Change to the specified directory
cd "$directory_path" || exit 1
# Iterate over files in the directory
for file in series_????_????-2.*; do
# Check if the item is a file
if [ -f "$file" ]; then
# Extract the file extension
basename=$file;
extension=
while [[ $basename = ?*.* ]]
do
extension=${basename##*.}.$extension
basename=${basename%.*}
done
extension=${extension%.}
# Extract the existing numbers from the filename
existing_numbers="${basename#series_}"
number1="${existing_numbers%%_*}"
number2="${existing_numbers#*_}"
# Zero pad the existing numbers
number1_padded=$(printf "%05d" "$number1")
number2_padded=$(printf "%05d" "$number2")
# Create the new filename with zero-padded numbers
new_filename="series_${number1_padded}_${number2_padded}.${extension}"
# Rename the file
mv "$file" "$new_filename"
echo "Renamed: $file to $new_filename"
fi
done
echo "Script executed successfully."