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: -

2
2009-10-21 13:53:59

What Others Think

That looks like hard work. What's wrong with: touch -r "$FILE1" "$FILE2"
flatcap · 469 weeks and 5 days ago
I didn't noticed.... Thank you!
kobayashison · 469 weeks and 5 days 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?

commandlinefu.com 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.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

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: