Files
paul-loedige a55c531b27
Deploy Hugo Site / build (push) Successful in 48s
added timestamp modification to knowledge base
2026-03-10 19:11:02 +01:00

68 lines
2.5 KiB
Markdown

+++
date = '2026-03-10T18:37:42+01:00'
draft = false
title = 'Modify Image Timestamp'
type='post'
tags = ['Linux', 'Photography']
+++
Some cameras don't automatically update their internal clock. This leads to images with "wrong" timestamps.
Modifying the [file metadata](https://en.wikipedia.org/wiki/Metadata) as well as the [Exif metadata](https://en.wikipedia.org/wiki/Exif) can fix this problem.
## Modifying the file metadata
```bash
touch -m -d "<date>" "<file_name>"
```
- `-m` only modify the modification date
- `-d "<date>"` use `<date>` instead of the current date
- `<file_name>` name of the file to be modified
### Example Script for adding an offset
The following command adds 20 years and 7 months while subtracting 6 days.
```bash
#!/bin/bash
original_mtime=$(stat -c %y "<file_name>")
new_mtime=$(date -d "$original_mtime +20 years +7 months -6 days" '+%Y-%m-%d %H:%M:%S')
touch -m -d "$new_mtime" "<file_name>"
```
- `stat -c %y "<file_name>"` reads the current modification timestamp from the file
- `date -d ...` calculates the new date
## Modifying the Exif metadata
```bash
exiftool -P -overwrite_original "-allDates=<date>" "<file_name>"
```
- `exiftool` [FOSS](https://en.wikipedia.org/wiki/Free_and_open-source_software) tool for viewing and modifying Exif data ([source](https://exiftool.org/))
- `-P` to not overwrite the file modification time (file metadata)
- `allDates=...` modifies the exif metadata dates
- can be used with operators (i.e. `+=`, `-=`) for offsets
### Example Script for adding an offset
The following command adds 20 years and 7 months while subtracting 6 days.
```bash
#!/bin/bash
exiftool -P -overwrite_original "-allDates+=20:7:0 0:0:0" "<file_name>"
exiftool -P -overwrite_original "-allDates-=0:0:6 0:0:0" "<file_name>"
```
## Example Script for Combined Modification
This script modifies all `.JPG` files in a given directory by adding 20 years and 7 months while subtracting 6 days.
```bash
#!/bin/bash
find "<dir_name>" -type f -iname "*.JPG" -print0 | while IFS= read -r -d '' file; do
echo "Processing $file"
#update meta data
original_mtime=$(stat -c %y "$file")
new_mtime=$(date -d "$original_mtime +20 years +7 months -6 days" '+%Y-%m-%d %H:%M:%S')
touch -m -d "$new_mtime" "$file"
#update exif data
exiftool -P -overwrite_original "-allDates+=20:7:0 0:0:0" "$file"
exiftool -P -overwrite_original "-allDates-=0:0:6 0:0:0" "$file"
done
echo "DONE!"
```
- `<dir_name>` name of the directory to be modified