The date difference of the new file when copying the file under different methods / systems, and the method of viewing the file creation time, modification time and access time

Time:2022-4-14

1: There was a dog headed man namedThe only shame, is the Earl of paradox, the master of Nintendo, Ubisoft’s bug, the pile driver of Polish donkey.
1: He has three wives, a windows, a Linux and a MacOS.
1: Every year the dog went up from his city to Shiloh to worship and sacrifice the king of armiesparameciumThere are two demands for productsGrasp file time information and system transplantation, whenparameciumThe oracle of.
1: On every day when the dog leader pays his salary, he distributes the salary to his children born to p society, Nintendo, Ubisoft and polish donkeys.
1: 5 to p club is double, because he loves P club. But he doesn’t finish the taskparameciumDon’t give the dog head a holiday.

To sum up, we need to help the dog headed people get the time information of files on different systems. It seems very simple, but there is a small detail similar to “\ n” and “\ R \ n”.

When copying files using command line or interface operation mode respectively under Mac, Linux and windows, it looks the same. The difference lies in whether the creation, modification and access time of the new file is consistent with that of the source file.

1、 Windows

First, under windows, when using PowerShell command line to copy files, it is as follows:

cp .\test.txt .\test3.txt

The results are as follows:

It can be seen that the new file inherits the modification time of the source file, but the creation time and access time are the time when it is copied and created; On the other hand, the creation time and access time of the source file have not changed.

When copying files manually with ctrl-c and ctrl-v, the results are consistent with the command line.

2、 Linux

Under Linux, use the command

cp test01.txt test02.txt

Copy the file before using the command

stat test01.txt

View the file information, and the information of the queried file will be printed directly on the screen:

Where, access is the file access time, modify is the last modification time of the file content, and change is the last modification time of the file attribute, corresponding to the timestamp atime / mtime / CTime respectively. The change time is special. The change time will change only when the name, size and permission of the file are changed.

However, please note that the creation time of the file, namely crtime, is not included. Looking at the source code, we can see that this is because there is no crtime in the inode structure:

So, if you want to check the creation time of the file, what do you need to do?
Take test01 Txt file as an example, first, use the command

stat test01.txt

View, or use

stat -c %i test01.txt

The inode number of the direct output file.
Then, use the command

df test01.txt | sed -n '2{s/ .*$//;p}'

Output the partition where the file is located. Finally, you can query the complete information of the file through debugfs:

debugfs -R 'stat <1839472>' /dev/mapper/centos-root


Through this method, you can query the new file again and find that the creation time of the new file is the time when it is copied and created:

3、 MacOS

On the terminal with 9:12touch HelloDogeCreate a file, edit and modify the file with the text provided by MAC at 9:13, open the file with text editing at 9:14, but make no modification, and use the command at 9:15cp HelloDoge HelloDoge2Copy the file.

As shown in the following figure, the new file inherits the opening (access) time of the source file, but the creation time and modification time are the time when it is copied and created; On other hand, creation time, modification time and access time of the source file have not changed.

Interestingly, in MacOS, the results of copying with the command line and right clicking in finder are different. Let’s try (excited).

It was found that the copied file perfectly inherited all the time information of the source file!

Buy one get one free moment

Note that when changing the modification time of the file, you must not use VIM, but use other methods (I use a text editor here). Because the. VIM file is created first SWP file (which means swap exchange) will replace the original file with SWP file when the w command is saved, so as to achieve the purpose of modification.

summary

system Copy method Creation time Modification time Visit time
Windows
command line Change to copy time inherit Change to copy time
Ctrl + C Change to copy time inherit Change to copy time
Linux command line Change to copy time Change to copy time inherit
MacOS
command line Change to copy time Change to copy time inherit
Ctrl + C inherit inherit inherit

P. S. there is also a change time in Linux, which means the attribute modification time. When copying, it will become the file copy time as the file modification time.

Final final

Finally, we helped the dog headed man solve this small problem, the world returned to peace, and the dog headed man could go back to Los Angeles to continue his humiliation and praise the Paramecium.