6

Please note that I am not asking how to change the creation date of a bunch of files to a given date. I want to shift the date by a given amount of time, for example 47732400 seconds.

Can I accomplish this using touch or something else in BASH?

2 Answers 2

6

touch only changes the creation time if the target modification time is before the original creation time.

for f in ~/Desktop/*; do
    old=$(stat -f %B -t %s "$f")
    touch -t $(date -r $(($old - 1234567)) +%Y%m%d%H%M%S) "$f"
done

SetFile always changes the creation time. It comes with the command line tools package that can be downloaded from Xcode's preferences or Apple's website.

for f in ~/Desktop/*; do
    old=$(stat -f %B -t %s "$f")
    new=$(date -r $(($old + 1234567)) '+%m/%d/%Y %H:%M:%S')
    SetFile -d "$new" -m "$new" "$f"
done

stat -f %B -t %s: format birth time, time format seconds since epoch
date -r: reformat seconds since epoch
touch -t: change access and modification times
SetFile -d: change creation time

5
  • Lauri Ranta, you are a saint! Commented Oct 25, 2012 at 19:15
  • I have to admit your script doesn't work for me, but the most important thing is that you introduced me to SetFile and got me well on my way. I'm also don't understand the reason behind the quotes around some of the variables. Totally new to bash scripting. Commented Oct 25, 2012 at 20:40
  • Quotes to ensure it works even if your filenames have spaces in them. Commented Oct 26, 2012 at 1:56
  • Figured that out eventually, also I take back my previous comment about the script not working for me. It does. I'm not sure what I did wrong at first but it was my fault. Thanks again! I can now fix a bunch of video files which were shot on a camera with an incorrect date setting. Commented Oct 26, 2012 at 11:00
  • If somebody is looking for a Linux equivalent, here are the modified commands in the for loop: old=$(stat -c %Y "$f") and touch -t $(date -d @$(($old - 47732400)) +%Y%m%d%H%M.%S) "$f". Commented Feb 17, 2015 at 1:23
0

Reusing and enhancing a previous answer to shift files dates with a reference file/date (the old GoPro always reset its time to 2009..)

ref_file=GOPR3440.MP4
new_time_for_that_file=0327160015 #date's format: 27th March 2015, 16:00

ref_file_timestamp=`stat -f %B -t %s "$ref_file"`
new_time_timestamp=`date -j $new_time_for_that_file +%s`
time_diff=$[$new_time_timestamp - $ref_file_timestamp]

for f in *; do
    old=$(stat -f %B -t %s "$f")
    new=$(date -r $(($old + $time_diff)) '+%m/%d/%Y %H:%M:%S')
    SetFile -d "$new" -m "$new" "$f"
done

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.