2

I know this is probably a real simple question, but I want to copy newer files from one server directory to another and preserve the datestamp.

I've tried cp -p /source/records_added*.csv /dest/targetdir and -pu and -pf The files that are being copied end up with the time they were copied to the new destination.

Is there a way to achieve this?

karel
  • 114,770
vswindell
  • 21
  • 2
  • 3
    use rsync it was made for this kind of copy Or TAR the files. tar can also keep the original timestamp. – Rinzwind Mar 13 '24 at 15:21
  • this explains cp https://askubuntu.com/questions/1040885/copy-a-file-and-keep-the-same-timestamp-of-the-original-file do check out https://askubuntu.com/a/1040886/15811 if you did cp without preserve on how to change the timestamp – Rinzwind Mar 13 '24 at 15:55
  • 3
    What are the source and destination filesystem types? How they are both mounted? What mount options are used for the destination filesystem? ... All those can affect setting file attributes in the destination filesystem and may prevent setting the time stamp by cp and set it by the destination filesystem instead. – Raffa Mar 13 '24 at 19:14

2 Answers2

5

The correct answer is cp -p source dest, as you already knew. So the correct question might be: Why aren't you seeing the times preserved? What you might not know is that there are, on a modern linux system, four distinct times, and only two can be preserved.

The times are:

A - Access time. This is automatically changed every time you read the file, though mount options can reduce this (typically to reduce wear on a flash drive). (This typically means the file should not be considered a candidate for moving to long term storage, but this is a feature Unix and Linux never implemented.)

M - Modify time. This is nominally the last time the data was changed. This typically means the file needs to be backed up again. This is the time normally shown by ls -l.

C - Create or Change time. This is the last time the inode was changed, which includes creating the file, extending it, and changing permissions or ownership. This was originally called the "Create" time, but since it changes easily, it is now known as the "Change" time.

B - Birth time. This is when you actually made this file. This is relatively new, and may not exist on all filesystems.

Now copying, through any method anyone has described, can only preserve Access and Modify times, and cp -p does so. You can examine all four times, possibly with nanosecond precision, using the stat command.

The other problem is that some filesystems might not honor some attempts to preserve times. As an obvious case, a FAT filesystem only has one time. Network file servers might also not honor certain types of change requests (and you did use the term "server").

One other point: If you are using cp -pu, then I would not expect the file to take on an older modification time. That's what the -u does. (i.e. it prevent the copy if the destination is newer.)

David G.
  • 151
  • 4
0

While David G's answer is undoubtedly correct, I'd like to offer a different way - cp couldn't always do this, and if you are lucky enough to work on older UNIX systems, this is the trick for copying from /source/dir to /target/dir:

(cd /source/dir;tar cf - *)|(cd /target/dir;tar xvf -)

To unpick this:

  • tar cf - * archives everything to a file -, ie stdout
  • tar xf - unpacks an archive from stdin
  • | connects one to the other, thus creating a pipeline that copies the source dir to the target

tar doesn't follow soft links, among other things, so there is less risk of copying a huge amount of data that you don't want. It also preserves ownership and permissions.

This is still my preferred way - I'm that old.

j4nd3r53n
  • 308