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.

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?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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

share|improve this answer
    
Lauri Ranta, you are a saint! –  bernk Oct 25 '12 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. –  bernk Oct 25 '12 at 20:40
    
Quotes to ensure it works even if your filenames have spaces in them. –  ckhan Oct 26 '12 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. –  bernk Oct 26 '12 at 11:00

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.