As detailed here, I am trying troubleshoot a script I've been working on to rename a series of pdfs with filenames in this format: The New Town Cryer - No. 1,032 [01 Oct 2020].pdf
to this 2020-10-01_-_The_New_Town_Cryer.pdf
. The script uses find
to find all PDFs in a given directory and then should rename them using a series of bash rematch commands. So far, the find
part of my script is correctly finding the PDFs, and the renaming script also works on its own. However, when I combine the two parts I get no output.
Original Script:
#!/bin/bash
set -x
find "$1" -name "*.pdf" -type f -printf '%f\0' | while IFS= read -r -d '' oldname; do #find all pdfs
: oldname="$oldname"
date_re='(^.*) - ([[:digit:]]{2}) ([[:alpha:]]+) ([[:digit:]]{4})(.*)'
if [[ $oldname =~ $date_re ]]; then
basename=${BASH_REMATCH[1]}
day=${BASH_REMATCH[2]}
month=${BASH_REMATCH[3]}
year=${BASH_REMATCH[4]}
ext=${BASH_REMATCH[5]}
new_date=$(date -d "${day} ${month} ${year}" +%Y-%m-%d)
newname="${new_date} - ${basename}${ext}"
newname=${newname//[[:space:]]/_}
echo "Old name: $oldname" # these lines will eventually be replaced with this line: mv "$oldname" "$newname"
echo "New name: $newname"
fi
done
I added set -x
to the beginning of my script, as well as : oldname="$oldname"
after do
, and then ran it using the command ./pub_renamer.sh /mnt/user/Pubs/Unsorted/The.New.Town.Cryer.-.No.1,032.[01.Oct.2020]
.
The result is this:
+ find '/mnt/user/Pubs/Unsorted/The.New.Town.Cryer.-.No.1,032.[01.Oct.2020]' -name '*.pdf' -type f -printf '%f\0'
+ IFS=
+ read -r -d '' oldname
+ : 'oldname=The New Town Cryer - No. 01,032 [01 Oct 2020].pdf'
+ date_re='(^.*) - ([[:digit:]]{2}) ([[:alpha:]]+) ([[:digit:]]{4})(.*)'
+ [[ The New Town Cryer - No. 01,032 [01 Oct 2020].pdf =~ (^.*) - ([[:digit:]]{2}) ([[:alpha:]]+) ([[:digit:]]{4})(.*) ]]
+ IFS=
+ read -r -d '' oldname
I also tried changing the %f
in the find command to %p
and running the script with the same directory as above and this is the output:
+ find '/mnt/user/Pubs/Unsorted/The.New.Town.Cryer.-.No.1,032.[01.Oct.2020]' -name '*.pdf' -type f -printf '%p\0'
+ IFS=
+ read -r -d '' oldname
+ : 'oldname=/mnt/user/Pubs/Unsorted/The.New.Town.Cryer.-.No.1,032.[01.Oct.2020]/The New Town Cryer - No. 01,032 [01 Oct 2020].pdf'
+ date_re='(^.*) - ([[:digit:]]{2}) ([[:alpha:]]+) ([[:digit:]]{4})(.*)'
+ [[ /mnt/user/Pubs/Unsorted/The.New.Town.Cryer.-.No.1,032.[01.Oct.2020]/The New Town Cryer - No. 01,032 [01 Oct 2020].pdf =~ (^.*) - ([[:digit:]]{2}) ([[:alpha:]]+) ([[:digit:]]{4})(.*) ]]
+ IFS=
+ read -r -d '' oldname
It seems that the newname isn't being matched from the oldname, but I'm not sure why.
Edit: This version of the script appears to be working now:
#!/bin/bash
find "$1" -name "*.pdf" -type f -printf '%f\0' | while IFS= read -r -d '' oldname; do #find all pdfs
: oldname="$oldname"
date_re='(^.*) - [^[]+\[([[:digit:]]{2}) ([[:alpha:]]+) ([[:digit:]]{4})\](.*)'
if [[ $oldname =~ $date_re ]]; then
basename=${BASH_REMATCH[1]}
day=${BASH_REMATCH[2]}
month=${BASH_REMATCH[3]}
year=${BASH_REMATCH[4]}
ext=${BASH_REMATCH[5]}
new_date=$(date -d "${day} ${month} ${year}" +%Y-%m-%d)
newname="${new_date} - ${basename}${ext}"
newname=${newname//[[:space:]]/_}
echo "Old name: $oldname"
echo "New name: $newname"
fi
done