Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
    
The keyword is mtime. However, I haven't found a solution yet, as I need a change relative to the file's mtime, not current time as described in unixtutorial.org/2008/11/… –  index Mar 22 '10 at 20:04

6 Answers 6

up vote 11 down vote accepted

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

share|improve this answer
    
yes, but this is relative to your current system time, not the file's.. thanks, though.. –  index Mar 22 '10 at 20:49
1  
When I first answered, yes, but I edited the answer. When you use the -r file option, it becomes relative to that file. So if you have the time be relative to the same file you're manipulating, you get what you want. –  rescdsk Mar 23 '10 at 14:54
    
Brilliant, thank you. Exactly what I've been looking for. –  Artem Russakovskii Apr 3 '11 at 9:45
    
@rescdsk, just out of curisoity, how would this change impact a forensic analysis of the file? if you modified a file, but then set its modified date back to the previous value, would this be detectable on the inode or other filesystem metadata? would journalling impact this analysis? –  Frank Thomas Aug 22 at 14:16
    
@FrankThomas, sorry, I've got no clue! –  rescdsk Aug 22 at 18:07

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.

share|improve this answer

Linux, using touch to change the last modified timestamp:

Create a file with timestamp of now:

el@apollo:~$ touch myfile.txt
el@apollo:~$ ll myfile.txt
-rw-rw-r-- 1 el el 0 Aug 22 09:25 myfile.txt

Change the timestamp to be 2 hours ago:

el@apollo:~$ touch -d "2 hours ago" myfile.txt
el@apollo:~$ ll myfile.txt
-rw-rw-r-- 1 el el 0 Aug 22 07:25 myfile.txt

Change the timestamp to be 200 hours ago:

el@apollo:~$ touch -d "200 hours ago" myfile.txt
el@apollo:~$ ll myfile.txt
-rw-rw-r-- 1 el el 0 Aug 14 01:25 myfile.txt

Change the timestamp to be 30 days ago:

el@apollo:~$ touch -d "30 days ago" myfile.txt
el@apollo:~$ ll myfile.txt
-rw-rw-r-- 1 el el 0 Jul 23 09:25 myfile.txt
el@apollo:~$

For shenanigans, set the last modified date in the future:

el@apollo:~$ touch -d "-400000 days ago" myfile.txt
el@apollo:~$ ll myfile.txt
-rw-rw-r-- 1 el el 0 Oct 21  3012 myfile.txt

Apparently, We gonna party like it's 3012 tonight.

share|improve this answer

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.

share|improve this answer

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 http://stackoverflow.com/questions/1158076/implement-touch-using-python

share|improve this answer
    
just to clarify, this means that corutil's date parser does not only accept relative, semantic times but actually can calculate! Just pointing it out as I did not find that information in the documentation (gnu.org/software/coreutils/manual/…) [It's actually there: "Relative items adjust a date (or the current date if none) forward or backward. The effects of relative items accumulate."] –  index Mar 23 '10 at 13:04

Use jhead:

http://www.sentex.net/~mwandel/jhead/usage.html

Adjust time stored in the Exif header by h:mm backwards or forwards. Useful when having taken pictures with the wrong time set on the camera, such as after travelling across time zones, or when daylight savings time has changed.

share|improve this answer
    
Thanks for the tip, this is similar to exif and exifTool and like digiKam's "Adjust Date and Time" feature -- but I would like to modify the file's system date. Another problem is that my smart OS seems to convert times to local time on the fly and DST has kicked in in one time zone :-) –  index Mar 22 '10 at 19:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.