109

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.

Peter Mortensen
  • 12,090
  • 23
  • 70
  • 90
Lazer
  • 17,227
  • 43
  • 116
  • 141

3 Answers3

128

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" *
Arjan
  • 30,974
  • 14
  • 75
  • 112
  • 35
    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](http://www.gnu.org/software/coreutils/manual/coreutils.html#cp-invocation) and on [FreeBSD](https://www.freebsd.org/cgi/man.cgi?query=cp) 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](https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/cp.1.html) additionally »Extended Attributes, including resource forks«. – Stefan Schmidt Jun 09 '15 at 15:14
  • 1
    on the latest OSX, cp -p still touches the source file's timestamp – afathman Feb 06 '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
  • i have ubuntu, nothing seems to work. -a, -p, --preserve=timestamps don't seem to preserve timestamp – thang Aug 09 '21 at 21:41
  • @thang, which date are you looking at? `ls -l` shows the following for me (with Ubuntu from `docker run -it ubuntu`): https://i.stack.imgur.com/evPZi.png – Arjan Aug 10 '21 at 10:26
20

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.

Stefan Schmidt
  • 623
  • 5
  • 12
  • 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. – B. Shea Jun 23 '17 at 15:36
15

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.

gorilla
  • 2,300
  • 14
  • 9
  • 2
    for inode change time, see also [linux - Setting creation or change timestamps - Stack Overflow](http://stackoverflow.com/questions/4537291/setting-creation-or-change-timestamps) – sdaau Oct 21 '13 at 11:08