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?

  • Why? If you want to "copy another files attributes," why not use touch -r
    – John1024
    Commented Jan 21, 2015 at 22:10
  • Yeah, I saw that but thats not what I want to do. I want to keep the string and maybe edit a folder and then change back the modification date etc. Commented Jan 21, 2015 at 22:16

4 Answers 4


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

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

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

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

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

  • 1
    Note that stat -c is specific to GNU stat. Most other stat implementations (including OS/X which the OP mentions) can specify the date format. GNU find's -printf can also specify the date format. Commented Jan 21, 2015 at 23:32
stat -f %m -t %Y%m%d%H%M.%S myfile

This prints the timestamp in the format required for touch -t.

Beware that the timestamp is expressed in the local timezone, which could be awkward to port files between timezones or for timestamps during the repeated hour of the summer-to-winter switch in timezones with DST. To avoid timezone trouble, use the UTC timestamp:

timestamp=$(TZ=UTC stat -f %m -t %Y%m%d%H%M.%S myfile)
TZ=UTC touch -t "$timestamp" myotherfile

Note that this applied to the BSD stat utility such as shipped with OSX, not to the GNU or BusyBox stat utilities such as found on Linux.

  • Note that to avoid issues around daylight saving switches, it's better to fix TZ to UTC0 for both touch and stat. Commented Jan 21, 2015 at 23:42

Doing "ls -otr --time-style=+%Y%m%d%H%M.%S" will get the date in the proper format, though with a bunch of other info that's easily edited out.

This is handy when assigned to an alias, and the output redirected to a file.

  • The OP tagged this OSX. --time-style is a feature of GNU coreutil's ls.
    – jordanm
    Commented Jan 22, 2015 at 3:33
  • I am on Ubuntu and this worked perfectly for me. Commented Jul 26, 2019 at 12:20

Since the question is tagged with , I will be assuming that the user is on macOS.

The touch utility on macOS is able to set the modification timestamp on a file when using its -m option. The issue now is to find a way to set the modification timestamp with touch -m on a number of files using some separate file as the "reference file".

The user suggests using stat to parse out the modification timestamp from the reference file, but the touch utility on macOS can do this by itself by using -r file:

touch -m -r reference_file file1 file2 file3

The above command sets the modification timestamp of file1, file2, and file3 to the same timestamp as the modification timestamp on the file called reference_file.

If the user wants to keep track of the original timestamp of a file or directory somewhere (as is alluded to in a comment), to be able to restore it later, they could simply create an empty file and set the timestamp on that with touch -m as mentioned above, with -r. At no time is it necessary to parse out the timestamp from stat.

# create an empty file to hold the original timestamp
touch -m -r original empty

# do stuff to the original

# restore timestamp on the original
touch -m -r empty original
rm empty

You must log in to answer this question.

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