103

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.

1
123

cp -p does the trick. For Linux:

-p same as --preserve=mode,ownership,timestamps

For FreeBSD:

-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" *
4
  • 31
    cp -a is also nice to know, it implies not only -p, but also -R to copy entire directories and -d to preserve links. – casualuser Feb 27 '10 at 20:53
  • 2
    Note though that when using the GNU Coreutils cp -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«. – Stefan Schmidt Jun 9 '15 at 15:14
  • 1
    on the latest OSX, cp -p still touches the source file's timestamp – afathman Feb 6 '17 at 18:15
  • 2
    As 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 Feb 18 '20 at 19:34
18

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.

1
  • 6
    This 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 Jun 23 '17 at 15:36
12

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.