This code is meant to search a set of folders containing files with name format DD-MM-YYYY and renames them as YYYY-MM-DD. However, while this loop works fine on the first iteration, it doesn't interpret the regular expression in the second run.
#!/usr/bin/env bash
WORKING_DIRS=( "/directory1" "/directory2" )
for WORKING_DIR in ${WORKING_DIRS[@]}
do
echo "WORKING DIRECTORY : $WORKING_DIR"
echo "--------------------------------------------------------"
for entry in "$WORKING_DIR"/[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9].doc
do
IFS='/'
read -r -a array <<< "$entry"
file_name="${array[${#array[@]}-1]}"
echo "$file_name"
IFS='.'
read -r -a file <<< "$file_name"
echo "OLD DATE: $file"
IFS='-'
read -r -a file_split <<< "${file[0]}"
day=${file_split[0]}
month=${file_split[1]}
year=${file_split[2]}
new_date="$year-$month-$day"
echo "NEW DATE : $new_date"
mv "$WORKING_DIR"/"$file_name" "$WORKING_DIR"/"$new_date".doc
done
done