Copy a file and keep the same timestamp of the original file
105,371
You can preserve the timestamp of the original file when copying using cp
by adding the -p
or --preserve
option:
-p same as --preserve=mode,ownership,timestamps --preserve[=ATTR_LIST] preserve the specified attributes (default: mode,ownership,time‐ stamps), if possible additional attributes: context, links, xattr, all
So to preserve only the timestamp
cp --preserve=timestamps oldfile newfile
or to preserve mode and ownership as well
cp --preserve oldfile newfile
or
cp -p oldfile newfile
Additional options are available for recursive copying - a common one is cp -a
(cp --archive
) which additionally preserves symbolic links.
Related videos on Youtube
Author by
Admin
Updated on September 18, 2022Comments
-
Admin almost 2 years
I need to a copy file and after that I need to change the timestamp attributes as the original file. How can I do it with the terminal or any other way?
-
bonh about 4 yearsSurprisingly, this did not work on macOS when copying from a FAT32 partition to an exFAT partition.
-
FlexMcMurphy almost 4 yearsI think this should be the accepted answer. It solves the problem with one command which I think is what the OP was really after. It is also well explained.
-
WinEunuuchs2Unix almost 4 yearsUnfortunately timestamps aren't exactly the same. Eg source stat:
Modify: 2020-08-31 14:32:41.481210326 -0600
aftercp --preserve=timestamps
target stat:Modify: 2020-08-31 14:32:41.000000000 -0600
. You can see nanoseconds are different which is a bug incp
I guess.