90
21
If I copy some file from some place to another using cp
, the timestamp on the copied file is set to the time of the copy.
Is there some way to avoid this?
I need to copy files without altering their timestamps.
90
21
If I copy some file from some place to another using cp
, the timestamp on the copied file is set to the time of the copy.
Is there some way to avoid this?
I need to copy files without altering their timestamps.
106
cp -p
does the trick. For Linux:
-p
same as--preserve=mode,ownership,timestamps
-p
Cause cp to preserve the following attributes of each source file in the copy: modification time, access time, file flags, file mode, ACL, user ID, and group ID, as allowed by permissions.
And for OS X:
-p
Cause cp to preserve the following attributes of each source file in the copy: modification time, access time, file flags, file mode, user ID, and group ID, as allowed by permissions. Access Control Lists (ACLs) and Extended Attributes (EAs), including resource forks, will also be preserved.
Note that this may/will change the source file's access time (atime), as shown by ls -lu
. Also, stat
or stat -x
can be used to nicely show the data access, data modification, and file status change times, to which for macOS the birth time can be added using explicit formatting:
stat -f "%n%N%nAccess (atime): %Sa%nModify (mtime): %Sm%nChange (ctime): %Sc%nBirth (Btime): %SB%n" *
16
When using cp
from the GNU Coreutils, to preserve only the timestamps and not attributes such as user id, group id or file mode there is the longhand --preserve
which allows to explicitly specify a list of attributes to be preserved.
cp --preserve=timestamps source destination
Be aware though that this syntax is probably not supported on other Unices. An alternative could be to use the --times
parameter of rsync
which should be available on most installations.
4This is correct answer. Using -p
is not the correct answer. -p
retains ownership & mode as well. Which may not be wanted.. and was not asked in question. – bshea – 2017-06-23T15:36:07.873
11
There are three times on a Unix filesystem, the access time (atime), the modification time (mtime), and the inode change time (ctime). You can change the access time and the modification time with the touch program, for example
cp orig copy touch -r orig copy
However, you cannot change the inode change time.
1
for inode change time, see also linux - Setting creation or change timestamps - Stack Overflow
– sdaau – 2013-10-21T11:08:59.4400
I recently needed to do something similar but using symlink instead.
To create a symlink and preserve the orignal timestamp: cp -ps src_file dst_symlink
As a comment this could be useful for a few readers, as an answer it adds noise for most readers. – ndemou – 2018-11-27T17:29:30.250
25cp -a is also nice to know, it implies not only -p, but also -R to copy entire directories and -d to preserve links. – casualuser – 2010-02-27T20:53:32.527
2
Note though that when using the GNU Coreutils
– Stefan Schmidt – 2015-06-09T15:14:13.593cp -p
not only preserves the time stamp but also mode and ownership and on FreeBSD besides the modification time it also preserves »access time, file flags, file mode, ACL, user ID, and group ID, as allowed by permissions.« and on OS X additionally »Extended Attributes, including resource forks«.on the latest OSX, cp -p still touches the source file's timestamp – afathman – 2017-02-06T18:15:18.960
1As afathman said, cp -p touches the source file's timestamp. That can be avoided by using the "noatime" mount option, such as "mount -o remount,noatime /mnt". – Greg Alexander – 2020-02-18T19:34:46.453