copy timestamps of files from one location to another - useful when file contents are already synced but timestamps are wrong.

find . -printf "touch -m -d \"%t\" '%p'\n" | tee /tmp/
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.
Sample Output
touch -m -d "Mon Nov  5 12:30:33 2012" './file1'
touch -m -d "Mon Nov  5 12:30:42 2012" './file2'
. . .

By: dmmst19
2012-11-05 20:32:05

What Others Think

I just realized that touch has a -c option for "no create". With this option it won't create any new files, only modify existing ones. That'd be helpful to add just in case you accidentally launch the script from the wrong location, or some of the copied files have been deleted/renamed.
dmmst19 · 389 weeks and 2 days ago
The originally posted version of this command accidentally grabbed the last *access* time of the source file rather than the *modification* time. The argument to -printf should be %t, not %a.
dmmst19 · 344 weeks and 4 days ago
I am a regular visitor of this website and so glad to see the details regarding the commands you have specified best online engagement rings store here. The above command is used to copy timestamps of a file from one location to another. The details are mentioned here and thank you so much for sharing such commands here.
Alyssalauren · 40 weeks and 6 days ago ??? ? ????? ????
seofox · 27 weeks and 1 day ago
seofox · 26 weeks and 2 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? 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: