-1

I am trying to shift the dates of a series of files by 9 hours. I've reached as far as this:

for i in *.MOV; do touch -r "$i" -d "-9 hours" "$i"; done

This should work in recent systems, but the touch command in OSX seems to be a bit outdated and not to support the -d switch.

I'm using Snow Leopard. Any idea on the best option for doing this with a single line command? I don't want to create a script for this.

Filipe Correia
  • 5,415
  • 6
  • 32
  • 47
  • As of OS X Mavericks, there's an -A option to touch to do this for you. – crb Mar 01 '14 at 22:11
  • Take a look at this: https://stackoverflow.com/questions/28400578/updating-file-created-date-by-x-number-of-days-mac-osx/28406484#28406484 and this: https://apple.stackexchange.com/questions/197895/how-to-do-a-batch-change-shift-of-file-creation-date – hawkeye Jan 06 '18 at 10:50
  • Someone should edit the question heading so it properly reflects what's actually being asked and the answer given. This currently comes up as the top Google result for '"osx" command line subtract 60 seconds from timestamp'. The heading is ambiguous and sounds like the answer will provide information to solve that, but the actual question and selected answer pertain solely to changing filesystem datestamps, not doing arithmetic on datestamps in general. This page doesn't tell me how to subtract time from a timestamp in BASH on OS X. – John Smith Jan 20 '21 at 05:47
  • @JohnSmith I believe the question and answer are clear enough but the `-d` switch allows to handle relative dates, which means you can effectively subtract a time span. – Filipe Correia Jan 21 '21 at 14:18

3 Answers3

1

Ok, sorted it out. OSX comes with a gtouch command, that knows the -d switch. It's part of GNU coreutils. See the comments below for information regarding availability on specific MacOS versions.

For more information on using relative dates with the -d switch see the manual.

Filipe Correia
  • 5,415
  • 6
  • 32
  • 47
  • As of OS X Mavericks, there's an -A option to touch to do this for you. – crb Mar 01 '14 at 22:10
  • Out of date, as well as uninformative. MacOS no longer contains gtouch. (At least, my copy of Mojave doesn't.) – John Smith Jan 20 '21 at 05:49
  • @JohnSmith It exists in my Mac OS Sierra, but I have no information about other OS versions. I've updated the answer to point to these informations in the comments. – Filipe Correia Jan 21 '21 at 14:25
0

Looking at the Wikipedia Page for Touch, it appears you're accustomed to the GNU version of Touch. Which MacOS isn't using.

For what you want to do, look into the "SetFile" command, which gets installed with XCode tools. You have -d and -m options, which reset the Created and Modified dates & times respectively.

http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/SetFile.1.html

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • Does SetFile allow to shift the date too? I see I can use SetFile's -d switch to set the date and time, but can't find one to shift it by a few hours – Filipe Correia Nov 06 '11 at 16:02
  • AFAIK, there is no "-d '-9 hours'" capability in SetFile. My suggestion would be to format the date (using thekashyap's answer) and then pass that in as the date & time to be set via SetFile. Look at the documentation link I posted and you can see the format SetFile is expecting a date & time to be set with. – Michael Dautermann Nov 06 '11 at 16:10
0

Donno OS X, but it should be easy enough to

get curr time stamp on the file
convert it to seconds
subtract 9 hours (9*60*60 secs) from it
convert it back to the format accepted by touch's -t option
run touch command

All this of course can be done in a single for loop on command line.

Here are simple examples from WikiPedia showing back and forth conversion.

# To convert a specific time stamp to Unix epoch time (seconds since 1970-01-01):
date +"%s" -d "Fri Apr 24 13:14:39 CDT 2009"
# 1240596879

# To convert Unix epoch time (seconds since 1970-01-01) to a human readable format:
date -d "UTC 1970-01-01 1240596879 secs"
# Fri Apr 24 13:14:39 CDT 2009

# Or:
date -ud @1000000000
# Sun Sep  9 01:46:40 UTC 2001

# or: Haven't tested this but should work..
date -d @1000000000 +%y%m%d%%H%M%S
# 010909014640
Kashyap
  • 15,354
  • 13
  • 64
  • 103