Set access and modification timestamps of a file using another one as reference

touch -r "$FILE1" "$FILE2"
atime and mtime timestamps of $FILE2 is changed according to the ones of $FILE1. If $FILE2 doesn't exist is created.
Sample Output
$ /tmp $ touch a1
$ /tmp $ touch a2
$ /tmp $ touch -r a1 a2
$ /tmp $ stat a?
  File: `a1'
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: fcffh/644444	Inode: 9266      Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/   apache)   Gid: ( 1000/   apache)
Access: 2013-04-01 20:34:13.968179377 +0200
Modify: 2013-04-01 20:34:13.968179377 +0200
Change: 2013-04-01 20:34:13.968179377 +0200
 Birth: -
  File: `a2'
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: fc09h/644444	Inode: 9817      Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/   apache)   Gid: ( 1000/   apache)
Access: 2013-04-01 20:34:13.968179377 +0200
Modify: 2013-04-01 20:34:13.968179377 +0200
Change: 2013-04-01 20:34:55.224383954 +0200
 Birth: -

2009-10-21 13:53:59

These Might Interest You

  • -a for access time, -m for modification time, -c do not create any files, -t timestamp

    touch -amct [[CC]YY]MMDDhhmm[.ss] FILE
    sharfah · 2009-05-27 14:33:22 0
  • touch -t 201208211200 first ; touch -t 201208220100 last ; creates 2 files: first & last, with timestamps that the find command should look between: 201208211200 = 2012-08-21 12:00 201208220100 = 2012-08-22 01:00 then we run find command with "-newer" switch, that finds by comparing timestamp against a reference file: find /path/to/files/ -newer first ! -newer last meaning: find any files in /path/to/files that are newer than file "first" and not newer than file "last" pipe the output of this find command through xargs to a move command: | xargs -ifile mv -fv file /path/to/destination/ and finally, remove the reference files we created for this operation: rm first; rm last;

    touch -t 201208211200 first ; touch -t 201208220100 last ; find /path/to/files/ -newer first ! -newer last | xargs -ifile mv -fv file /path/to/destination/ ; rm first; rm last;
    ktopaz · 2012-08-22 09:51:40 0
  • This is useful when watching a log file that does not contain timestamps itself. If the file already has content when starting the command, the first lines will have the "wrong" timestamp when the command was started and not when the lines were originally written.

    tail -f file | while read line; do echo -n $(date -u -Ins); echo -e "\t$line"; done
    hfs · 2010-11-19 10:01:57 4
  • Sometimes when copying files from one place to another, the timestamps get lost. Maybe you forgot to add a flag to preserve timestamps in your copy command. You're sure the files are exactly the same in both locations, but the timestamps of the files in the new home are wrong and you need them to match the source. Using this command, you will get a shell script (/tmp/ than you can move to the new location and just execute - it will change the timestamps on all the files and directories to their previous values. Make sure you're in the right directory when you launch it, otherwise all the touch commands will create new zero-length files with those names. Since find's output includes "." it will also change the timestamp of the current directory. Ideally rsync would be the way to handle this - since it only sends changes by default, there would be relatively little network traffic resulting. But rsync has to read the entire file contents on both sides to be sure no bytes have changed, potentially causing a huge amount of local disk I/O on each side. This could be a problem if your files are large. My approach avoids all the comparison I/O. I've seen comments that rsync with the "--size-only" and "--times" options should do this also, but it didn't seem to do what I wanted in my test. With my approach you can review/edit the output commands before running them, so you can tell exactly what will happen. The "tee" command both displays the output on the screen for your review, AND saves it to the file /tmp/ Credit: got this idea from Stone's answer at, and combined it into one line. Show Sample Output

    find . -printf "touch -m -d \"%t\" '%p'\n" | tee /tmp/
    dmmst19 · 2012-11-05 20:32:05 2

What Others Think

That looks like hard work. What's wrong with: touch -r "$FILE1" "$FILE2"
flatcap · 452 weeks and 1 day ago
I didn't noticed.... Thank you!
kobayashison · 452 weeks and 1 day ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: