linux/shell: change a file's modify timestamp relatively?

My camera produces files like IMG_1234.JPG and MVI_1234.AVI with timestamps on those files. Unfortunately the time wasn't set properly and timestamps are off.

I would like to set the file's timestamp on disk. (not the EXIF data).

Proposed algorithm:

1 read file's modify date
2 add delta, i.e. hhmmss (preferred: change timezone)
3 write new timestamp

Is there an easy way to do this? maybe one could simplify the calculation using epoch time (seconds since) and whip up a shell script.


touch can do this:

 $ ls -l something
-rw-rw-r-- 1 tgs tgs 0 2010-03-22 16:03 something
 $ touch -r something -d '-1 day' something 
 $ ls -l something 
-rw-rw-r-- 1 tgs tgs 0 2010-03-21 16:03 something

http://linux.about.com/library/cmd/blcmdl_touch.htm

To change the mtime, add --time=mtime


Combining the above, if AM/PM was wrong...

Correct the file time stamps:

#!/bin/sh
for i in all/*; do
  touch -r "$i" -d '-12 hour' "$i"
done

Then update the EXIF info in the jpg files to the corrected time stamp:

$ jhead -dsft *.jpg

Don't forget to fix the time setting in your camera.


iterates over all files in the subdirectory all: use stat to get the files epoch / unix time in seconds, let touch parse the sum as new date for mtime and write to file

#!/bin/sh
for i in all/*; do
  touch -m -d "$(stat -c %y "$i") + 3600 sec" "$i"
done

for a pythonian approach see https://stackoverflow.com/questions/1158076/implement-touch-using-python


I am doing the same thing in OS X, and the syntax of touch varies here a bit.

I am using:

touch -r "filename" -A '013007' "filename"

This will adjust +1hour 30min 7sec relative to the original modified time. '-013007' for turning the time back.