Skip to content

Instantly share code, notes, and snippets.

@federicoB
Last active June 4, 2023 13:50
Show Gist options
  • Save federicoB/98a49d1365050528f0ae0f4e2922b187 to your computer and use it in GitHub Desktop.
Save federicoB/98a49d1365050528f0ae0f4e2922b187 to your computer and use it in GitHub Desktop.
Bash script for setting exif photo date according to filename. Created for whatsapp images can be easily adjusted for every filename patterns.
#!/bin/bash
set -e #Exit as soon as any line in the bash script fails
imageCounter=0
for filename in *.jpg
do
imageCounter=$((imageCounter+=1))
#extracting info from file name
year="${filename:4:4}"
month="${filename:8:2}"
day="${filename:10:2}"
#setting exif timestamp into file
exiv2 -M"set Exif.Image.DateTime $year:$month:$day" $filename
#set the file last modified timestamp
touch -t "$year$month$day""1500" $filename
done
echo "successfully set the correct timestamp in $imageCounter images"
@nkuk377
Copy link

nkuk377 commented Dec 22, 2022

Hi.
I was trying to sort this problem out and found this code which helped me a lot. It worked like charm without changing anything.
I have 100-s of photos with right date-time on their names, but wrong in exif data.
I tried to modify this code to set the "time" stamp as in the photo's name, but could not make it work.
I stuck on error: "touch: invalid date format ‘20220412 200618’"
The example file I am trying this on is "IMG_20220412_200618.jpg"
I declared 3 more variables: $hour, $min, $sec, then tweak around, but no luck.
Please, give me a clue how to implement this.
Thanks for your code =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment