How to copy files and directories fully preserving nanosecond precision for timestamps

LinuxRocks

New Member
Joined
Sep 26, 2020
Messages
7
Reaction score
1
Credits
78
Purchased a new laptop recently, so need to copy files from old laptop to new.

Used the tar command to create an archive, then copied the tar file across to external hard drive, afterwards copied the tar file from external drive to new laptop, then extracted archive on new laptop.

Once extracted to proper location on new laptop. e.g. ~/Music. I did:

Code:
ls -la --time-style=full-iso

to check timestamps of the directories. This was the output:

Code:
[w@wt Music]$ ls -la --time-style=full-iso

total 28
drwxrwxrwx 7 k k 4096 2017-05-13 00:09:35.000000000 +0700  .
drwxr-xr-x 3 k k 4096 2020-09-24 14:59:04.748455435 +0700  ..
drwxrwxrwx 3 k k 4096 2017-05-13 00:09:33.000000000 +0700 'Amy MacDonald'
drwxrwxrwx 2 k k 4096 2017-05-13 00:09:33.000000000 +0700 'Black Holes and Revelations'
drwxrwxrwx 3 k k 4096 2017-05-13 00:09:34.000000000 +0700 'Kings Of Leon'
drwxrwxrwx 2 k k 4096 2017-05-13 00:09:32.000000000 +0700 'Learn Thai'
drwxrwxrwx 3 k k 4096 2017-05-13 00:09:35.000000000 +0700 'Richard Wagner'

I went back to old laptop and issued same command:

Code:
ls -la --time-style=full-iso

Output was:

Code:
drwxrwxr-x  7 k    k    4096 2017-05-13 00:09:35.699641876 +0700 .
drwxrwxr-x 14 root adm 20480 2020-09-24 23:58:56.950677706 +0700 ..
drwxrwxr-x  3 k    k    4096 2017-05-13 00:09:33.555626045 +0700 Amy MacDonald
drwxrwxr-x  2 k    k    4096 2017-05-13 00:09:33.547625987 +0700 Black Holes and Revelations
drwxrwxr-x  3 k    k    4096 2017-05-13 00:09:34.223630978 +0700 Kings Of Leon
drwxrwxr-x  2 k    k    4096 2017-05-13 00:09:32.671619517 +0700 Learn Thai
drwxrwxr-x  3 k    k    4096 2017-05-13 00:09:35.755642289 +0700 Richard Wagner

Notice how the timestamps are ever so slightly different. Tar isn't preserving nanosecond precision when it creates the archive or when it's extracted. BTW, I am using the '--atime-preserve' switch when extracting the archive:

Code:
tar --atime-preserve -xvpf Music.tar

If I use the 'cp' command to copy to external drive:

Code:
cp -ar ~/Music /media/ext_drive/backup

then do:

Code:
ls -la --time-style=full-iso

to check timestamps on the copied files of the external drive, output is:

Code:
[w@wt Music-copy]$ ls -la --time-style=full-iso
total 28
drwxrwxrwx 7 k k 4096 2017-05-13 00:09:35.699641800 +0700 .
drwxr-xr-x 4 k k 4096 2020-09-27 15:11:23.603036057 +0700 ..
drwxrwxrwx 3 k k 4096 2017-05-13 00:09:33.555626000 +0700 'Amy MacDonald'
drwxrwxrwx 2 k k 4096 2017-05-13 00:09:33.547625900 +0700 'Black Holes and Revelations'
drwxrwxrwx 3 k k 4096 2017-05-13 00:09:34.223630900 +0700 'Kings Of Leon'
drwxrwxrwx 2 k k 4096 2017-05-13 00:09:32.671619500 +0700 'Learn Thai'
drwxrwxrwx 3 k k 4096 2017-05-13 00:09:35.755642200 +0700 'Richard Wagner'

It's close to the original, but notice how the last four digits of 'Black Holes and Revelations' has '5900' when the original has '5987'.

I've tried using the rar command as well. It was the same as using the tar command. i.e. didn't preserve nanosecond precision at all.

Also tried, drag and drop copy using the windowed interface on xfce. It was the same as the 'cp -ar' copy. i.e. last two digits not preserved. Maybe the default xfce file manager uses the ‘cp -ar’ in the background to copy?

Any idea how to copy files and directories fully preserving nanosecond precision?

Linux distro and version of old laptop is: Ubuntu 16.04

Thanks,
 
Last edited:


Try man cp. I think this is what you need:
--preserve[=ATTR_LIST]
preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all
 
Try man cp. I think this is what you need:
--preserve[=ATTR_LIST]
preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all
Did go through man page of cp then try various options. Pretty sure I tried --preserve.

I'll try it again.

If it's a no go, would I be able to boot using a usb live CD of newer distro.e.g. ubuntu 20.04 then use the cp command of that distro in single-use mode. Perhaps something worth trying?
 
The last two digits are lost on copying to external drive. Here's why

I did a test by copying (using cp -ar) the Music directory to ~/temp of the old computer. timestamps were preservered exactly.

Code:
drwxrwxr-x  2 k    k    4096 2017-05-13 00:09:33.547625987 +0700 Black Holes and Revelations

Tried copying to external drive using same command:

Code:
cp -ar ~/Music /media/ext_drive/backup

output of ls:

Code:
drwxrwxrwx 2 k k 4096 2017-05-13 00:09:33.547625900 +0700 'Black Holes and Revelations'

tried copying from old computer to USB pen drive. Funnily enough, that was the same as doing the tar process:

Code:
drwxrwxrwx 2 k k 4096 2017-05-13 00:09:33.000000000 +0700 'Black Holes and Revelations'

Checked the partition type of external drive. It's NTFS. checked partition type of USB pen drive. That's W95 FAT32.

Could be something to do with partition type. Will be using a proper file filesystem type when starting out (formatting) an external drive or USB pen drive from now on,
 
Didn't realize you were using FAT32/NTFS. Try ext4 or xfs and see what happens then? Also you could play around with some of the preserve options of rsync, man rsync.
 
Last edited:
Didn't realize you were using FAT32/NTFS. Try ext4 or xfs and see what happens then? Also you cold play around with some of the preserve options of rsync, man rsync.
same result as cp -ar when I do:

Code:
rsync -avihXP ~/Music /media/ext_drive/backup/test

to copy to external drive. Last two digits lost:

Code:
drwxrwxrwx 2 k k 4096 2017-05-13 00:09:33.547625900 +0700 'Black Holes and Revelations'
 
Dug out my other external drive which has hard disk from old netbook computer. All partitions ext4. copied Music directory, subdirectory and files using 'cp':

Code:
cp -ar ~/Music /media/old_netbook_drive/backup

after copying issued:

Code:
ls -la --time-style=full-iso

output:

Code:
[w@wt Music]$ ls -la --time-style=full-iso
total 28
drwxrwxr-x  7 k k 4096 2017-05-13 00:09:35.699641876 +0700  .
drwxr-xr-x 17 k k 4096 2020-09-29 23:15:30.542251368 +0700  ..
drwxrwxr-x  3 k k 4096 2017-05-13 00:09:33.555626045 +0700 'Amy MacDonald'
drwxrwxr-x  2 k k 4096 2017-05-13 00:09:33.547625987 +0700 'Black Holes and Revelations'
drwxrwxr-x  3 k k 4096 2017-05-13 00:09:34.223630978 +0700 'Kings Of Leon'
drwxrwxr-x  2 k k 4096 2017-05-13 00:09:32.671619517 +0700 'Learn Thai'
drwxrwxr-x  3 k k 4096 2017-05-13 00:09:35.755642289 +0700 'Richard Wagner'

copied Music directory, subdirectory and files to new laptop using same 'cp' command above. Checked timestamps using same 'ls' command as above.

Output:

Code:
total 28
drwxrwxr-x  7 k k 4096 2017-05-13 00:09:35.699641876 +0700  .
drwxr-xr-x 17 k k 4096 2020-09-29 23:15:30.542251368 +0700  ..
drwxrwxr-x  3 k k 4096 2017-05-13 00:09:33.555626045 +0700 'Amy MacDonald'
drwxrwxr-x  2 k k 4096 2017-05-13 00:09:33.547625987 +0700 'Black Holes and Revelations'
drwxrwxr-x  3 k k 4096 2017-05-13 00:09:34.223630978 +0700 'Kings Of Leon'
drwxrwxr-x  2 k k 4096 2017-05-13 00:09:32.671619517 +0700 'Learn Thai'
drwxrwxr-x  3 k k 4096 2017-05-13 00:09:35.755642289 +0700 'Richard Wagner'

Solved.
 
Notice how the timestamps are ever so slightly different. Tar isn't preserving nanosecond precision when it creates the archive or when it's extracted. BTW, I am using the '--atime-preserve' switch when extracting the archive:
Just discovered a solution for using tar to store/compress then extract preserving nanosecond precision using POSIX.

When storing/compressing do:

Code:
tar --format=posix -cvpf filename.tar directory-name-to-tar

extract preserving nanosecond precision timestamps:

Code:
tar --atime-preserve -xvpf filename.tar
 
Hello, I have alot of folders and files with documents, vacation photos that I want to replicate on my NTFS hard drive. If I format a external hard drive with ext4, the same filesystem as android, can date created, date modified and date accessed of these files and folders be preserved on windows 10 ? Any suggestions would help plz and thank you
 

Members online


Latest posts

Top