40

I need to copy file and after that I need to change timestamp attributes as original file. How to do it with terminal or any other way.

| improve this question | |
  • 8
    Why "after that", specifically? you can preserve the timestamp (and other attributes) during copying by using the -p or --preserve= option e.g. cp -p oldfile newfile – steeldriver May 27 '18 at 13:23
  • 4
    @steeldriver Technically cp itself also does it afterwards. Please make cp --preserve=timestamps an answer – Sebastian Stark May 27 '18 at 13:30
18

If you want to preserve the original timestamps, use

$ touch -r <original_file> <new_file>

This copies the timestamps from another file.

See this blog post for more: Fake File Access, Modify and Change TimeStamps

| improve this answer | |
70

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.

| improve this answer | |
  • Surprisingly, this did not work on macOS when copying from a FAT32 partition to an exFAT partition. – bonh May 7 at 1:04

Your Answer

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