Batch change creation dates based on file names

filerenameterminalunix

I have files that are named like "12. 2017-04-11 063118.jpg", and "123. 2016-09-05 115236.jpg". The number to the left of the "." counts up like an index from 1 to 1400.

How can I set the creation date and time according to how it is specified in the file name for all 1400 files I have in the folder. I know the command touch -t changes the creation date, but I do not know how to extract the date/time information from the file name and put this into a command so this is done automatically for all files.

I know from another thread that the below should work. but i don't know how to tweak the text conversion to correctly apply to my file naming convention.

for f in *; do
    t=$(echo $f | sed -E 's/([A-z]*-)|([ ,;])|(\..*)//g' | sed -E 's/(.*)(..)/\1.\2/')
    touch -t $t "$f"
done

The above code works if my file naming convention is like "clip-2014-01-31 18;50;15.mp4", however, I have a different format: "12. 2017-04-11 063118.jpg", and "123. 2016-09-05 115236.jpg". Does anyone know how to tweak the sed function command to process the conversion into the correct format for touch?

Best Answer

Bash parameter expansion will work:

for f in *; do
    t="${f#* }"
    t="${t%%.*}"
    t="${t:0:4}${t:5:2}${t:8:2}${t:11:2}${t:13:2}.${t:15:2}"
    touch -t $t "$f"
done
  • t="${f#* }" strips off the part before (< the first space)
  • t="${t%%.*}" strips off the part after .
  • t="${t:0:4}${t:5:2}${t:8:2}${t:11:2}${t:13:2}.${t:15:2}" formats the content of the date variable (t) for touch which requires the following format: [[CC]YY]MMDDhhmm[.SS].

And as one-liner:

for f in *; do t="${f#* }"; t="${t%%.*}"; t="${t:0:4}${t:5:2}${t:8:2}${t:11:2}${t:13:2}.${t:15:2}"; touch -t $t "$f"; done