2

I have about 100 .jpeg images that I would like to change the file time stamp for. I have seen many examples of using touch to modify the time stamp based on the number of days, hours, etc., but those do not work in my situation.

I have attempted to use touch -d "-1969 days ago" on one of the files, but the new time was way off -- Jul 10 2021 rather than May 25 2014.

Is there any way to change all file time stamps in a directory to a specific date (2014-05-25) while keeping the time portion (2:30 PM in the example below) of the date time stamp?

For example, 2009-01-02 02:30 PM change timestamp to 2014-05-25 2:30 PM.

Thank you.

0

1 Answer 1

3

You could use a combination of date and touch, here GNU coreutils.

If I read you correctly you want a specific date and keep file time, as in HMS. Not set each file say 300 days prior to the current value.

You can create that time by combining the string 2014-05-25 with the extracted time from the files by using date, for example:

$ date +"2014-05-25 %T.%N" -r file.jpg
2014-05-25 18:06:28.277679656

Then combine by doing something like:

for f in *.jpg; do touch -d "$(date +"2014-05-25 %T.%N" -r "$f")" "$f"; done

Note that this can mess with DST and timezone.

An alternative might be to add %z at end of date string, or add -u to date options.

TZ=UTC0 touch -d "$(date -u +"2014-05-25 %T.%N" -r "$f")"

Test

Test, test, test before action. Check with ls --full-time, echo instead of touch etc. find ... -printf also have time/date options.

You could even do a backup of times to a text file.


Notes:

From Q:

  • 1969 days ago is 29 Sep 2010.
  • 1969 days into the future is Jul 11 2021 (at least in my time zone).
  • To get May 25 2014 from "-1969 days ago" current date has to be 15 Oct 2019.

The issue with "-1969 days ago" is that you have a minus in front of the time.

Minus + minus = plus

Either remove the minus sign, or remove the  ago part.

1969 days ago is 29 Sep 2010, as is -1969 days

1
  • Thank you for your answer and helpful comments. for f in *.jpg; do TZ=UTC0 touch -d "$(date -u +"2014-05-25 %T.%N" -r "$f")" "$f"; doneworked perfectly for me.
    – iembry
    Feb 20, 2016 at 4:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .