Get modification date that can be used with touch -m

datefilesosxtimestampstouch

I need to get the modification date of a file so that I can set it on other files using touch -m.

stat or perl -le 'print((stat shift)[9])' Didn't return what I wanted. I think that you are supposed to use – and + but I'm not sure how to use it correctly.

I though that using the -t I though it could set it another way, here I tried to set it to 2013 July the 3rd.

touch -t  20130703

But that didn't change it the way I want either. So back to the question, how do I copy another files attributes so that I can set them with touch -m?

Best Answer

You may convert the time returned by stat or perl to the format you want with the command date (assuming you have GNU coreutils installed):

# Convert UNIX time returned by perl to year+month+day
$ date -d @$(perl -le 'print((stat shift)[9])' FILENAME) +%Y%m%d
20130703

# Convert formatted time returned by GNU stat to year+month+day
$ date -d "$(stat -c %y FILENAME)" +%Y%m%d
20130703

date itself can give you the modification time for files directly too:

$ date -r FILENAME +%Y%m%d
20130703

For details, see the man page for GNU date (man date).

Related Question