-   Programming (
-   -   BASH: Keep original mod date of a file and 'date' it back to same file? (

SilversleevesX 07-12-2010 09:08 PM

BASH: Keep original mod date of a file and 'date' it back to same file?
I would really like to preserve a file's original modified date and pass it back to the file as the same attribute after a script has worked on it. I get a lot of JPEG files from different places on the Net which I either turn around and upload or burn to disk, and having the "original" date of either download or last mod in a graphics app would be for me, in the long run, a lot more helpful when deciding, for instance, which files to "recycle" or pass on backing up more than once.

I've tried doing this on my own every now and then. Where I run into problems is that it appears "stat" and "date" use different formats for date information, and I can't seem to puzzle out how to "translate" one to the other satisfactorily for the latter command.

Just to give an example:
stat foo.jpg |grep Modify gives me
Modify: 2010-07-12 06:28:56.890625000 -0400

Passing that string as-is to date foo.jpg, I get the error
date: unknown option -- 0 and the usual semi-courteous suggestion to Try 'date --help' for more information.

Somehow my TexInfo database got screwed up somewhere along the line and
info date
gives me the short article on date input formats, not the full documentation for the command. So any help in this regard would be much appreciated.


kbp 07-12-2010 09:11 PM

Try 'touch -m -d <date_string> <filename>'

SilversleevesX 07-16-2010 10:45 AM


Originally Posted by kbp (Post 4031414)
Try 'touch -m -d <date_string> <filename>'

Works well enough. I'd call that a solution.
Forgot that 'touch' had those extra functions. I've been using it to create empty files into which to save data from other apps (so I don't have to type in a file name, just click on the empty and confirm the overwrite).
Time to re-acquaint myself with the MAN page, methinks. :)


grail 07-16-2010 10:53 AM

Well I can see this is SOLVED (please mark it as so), but in case you are interested:

date -d "$(stat foo.jpg | sed -n '/Modify/s/[^:]*: //p')"

colucix 07-16-2010 11:12 AM

My :twocents:

date -r foo.jpg

All times are GMT -5. The time now is 02:34 PM.