Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 31 down vote accepted

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.

share|improve this answer
10  
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
1  
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

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.

share|improve this answer
    
for inode change time, see also linux - Setting creation or change timestamps - Stack Overflow – sdaau Oct 21 '13 at 11:08

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.

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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