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.)
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:55cp
and set it by the destination filesystem instead. – Raffa Mar 13 '24 at 19:14