fork download
  1. set -x
  2.  
  3. oldname='The New Town Cryer - 01 Oct 2020.pdf'
  4. date_re='(^.*) - ([[:digit:]]{2}) ([[:alpha:]]+) ([[:digit:]]{4})(.*)'
  5. if [[ $oldname =~ $date_re ]]; then
  6. basename=${BASH_REMATCH[1]}
  7. day=${BASH_REMATCH[2]}
  8. month=${BASH_REMATCH[3]}
  9. year=${BASH_REMATCH[4]}
  10. ext=${BASH_REMATCH[5]}
  11. new_date=$(date -d "${day} ${month} ${year}" +%Y-%m-%d)
  12. newname="${new_date} - ${basename}${ext}"
  13. echo "Old name: $oldname"
  14. echo "New name: $newname"
  15. fi
  16.  
Success #stdin #stdout #stderr 0s 4556KB
stdin
Standard input is empty
stdout
Old name: The New Town Cryer - 01 Oct 2020.pdf
New name: 2020-10-01 - The New Town Cryer.pdf
stderr
+ oldname='The New Town Cryer - 01 Oct 2020.pdf'
+ date_re='(^.*) - ([[:digit:]]{2}) ([[:alpha:]]+) ([[:digit:]]{4})(.*)'
+ [[ The New Town Cryer - 01 Oct 2020.pdf =~ (^.*) - ([[:digit:]]{2}) ([[:alpha:]]+) ([[:digit:]]{4})(.*) ]]
+ basename='The New Town Cryer'
+ day=01
+ month=Oct
+ year=2020
+ ext=.pdf
++ date -d '01 Oct 2020' +%Y-%m-%d
+ new_date=2020-10-01
+ newname='2020-10-01 - The New Town Cryer.pdf'
+ echo 'Old name: The New Town Cryer - 01 Oct 2020.pdf'
+ echo 'New name: 2020-10-01 - The New Town Cryer.pdf'