8

I have a directory full of photos with file names in this format: IMG-20160305-WA0001.jpg. The date taken is obviously in the file title. Unfortunately the date modified on all files is today. I want to set them back to the correct date.

I am thinking of a bash script that would extract the date portion in the name and then for example touch -a -m -t 201603050900 IMG-20160305-WA0000.jpg for each file in turn (using correct date for each one). The time of day does not matter.

Jodel
  • 81

2 Answers2

10

Example using bash string manipulation only to extract the date:

#!/bin/bash

for name in IMG-[0-9]*.jpg; do
    touch -amt ${name:4:8}0900 "$name"
done
Guido
  • 4,114
2

From your example, assuming that all of the files have a valid yyyymmdd date, you can extract the date from the filename and apply that in the command cited:

#!/bin/bash
for name in IMG-*-W*.jpg
do
    date="$(echo "$name" | sed -e 's/^IMG-//' -e 's/-W.*//')"
    touch -a -m -t ${date}0900 "$name"
done

If some file hasn't a valid date, that is more work. But you can test that in bash with a regular expression.

Thomas Dickey
  • 76,765