如果内容修改了如何获取最后修改时间

如果内容修改了如何获取最后修改时间

在 Ubuntu 中,如果用户没有写入文件的权限,但如果他尝试更改,则该文件的最后更新时间将会更改(即使在出现写入错误:不允许操作之后)。所以,我想要文件的最后修改时间,如果用户成功保存或修改内容是可能的。

答案1

该命令stat为您提供该信息。您可以看到三个时间戳。访问、修改和更改。通过(成功)读取文件,Access 被修改。通过更改例如权限,可以修改更改。如果内容改变了,Modify就改变了。看看这个:

#case@maru:~/temp:[0]$ rm test 
#case@maru:~/temp:[0]$ echo test>test;stat test
  File: test
  Size: 5           Blocks: 8          IO Block: 4096   regular file
Device: 806h/2054d  Inode: 21627629    Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/    case)   Gid: ( 1000/    case)
Access: 2019-03-04 17:52:39.715956782 +0000
Modify: 2019-03-04 17:52:39.715956782 +0000
Change: 2019-03-04 17:52:39.715956782 +0000
 Birth: -

文件已创建。所有时间戳都是相同的。

#case@maru:~/temp:[0]$ echo test2>test;stat test
  File: test
  Size: 6           Blocks: 8          IO Block: 4096   regular file
Device: 806h/2054d  Inode: 21627629    Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/    case)   Gid: ( 1000/    case)
Access: 2019-03-04 17:52:39.715956782 +0000
Modify: 2019-03-04 17:52:53.739979011 +0000
Change: 2019-03-04 17:52:53.739979011 +0000
 Birth: -

内容已修改。修改和更改已更改。

#case@maru:~/temp:[0]$ cat test ; stat test 
test2
  File: test
  Size: 6           Blocks: 8          IO Block: 4096   regular file
Device: 806h/2054d  Inode: 21627629    Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/    case)   Gid: ( 1000/    case)
Access: 2019-03-04 17:53:11.676006377 +0000
Modify: 2019-03-04 17:52:53.739979011 +0000
Change: 2019-03-04 17:52:53.739979011 +0000
 Birth: -

文件已读取。访问权限已更改。

#case@maru:~/temp:[0]$ chmod 0000 test ; stat test
  File: test
  Size: 6           Blocks: 8          IO Block: 4096   regular file
Device: 806h/2054d  Inode: 21627629    Links: 1
Access: (0000/----------)  Uid: ( 1000/    case)   Gid: ( 1000/    case)
Access: 2019-03-04 17:53:11.676006377 +0000
Modify: 2019-03-04 17:52:53.739979011 +0000
Change: 2019-03-04 17:54:13.792092496 +0000
 Birth: -

改变烫发。更改已修改。

#case@maru:~/temp:[0]$ echo test3>test;stat test
bash: test: Permission denied
  File: test
  Size: 6           Blocks: 8          IO Block: 4096   regular file
Device: 806h/2054d  Inode: 21627629    Links: 1
Access: (0000/----------)  Uid: ( 1000/    case)   Gid: ( 1000/    case)
Access: 2019-03-04 17:53:11.676006377 +0000
Modify: 2019-03-04 17:52:53.739979011 +0000
Change: 2019-03-04 17:54:13.792092496 +0000
 Birth: -

修改失败。没有变化

#case@maru:~/temp:[0]$ cat test ; stat test 
cat: test: Permission denied
  File: test
  Size: 6           Blocks: 8          IO Block: 4096   regular file
Device: 806h/2054d  Inode: 21627629    Links: 1
Access: (0000/----------)  Uid: ( 1000/    case)   Gid: ( 1000/    case)
Access: 2019-03-04 17:53:11.676006377 +0000
Modify: 2019-03-04 17:52:53.739979011 +0000
Change: 2019-03-04 17:54:13.792092496 +0000
 Birth: -

阅读失败。没有变化。

#case@maru:~/temp:[0]$ chmod 0644 test ; stat test
  File: test
  Size: 6           Blocks: 8          IO Block: 4096   regular file
Device: 806h/2054d  Inode: 21627629    Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/    case)   Gid: ( 1000/    case)
Access: 2019-03-04 17:53:11.676006377 +0000
Modify: 2019-03-04 17:52:53.739979011 +0000
Change: 2019-03-04 17:55:45.860198022 +0000
 Birth: -

恢复烫发。更改已修改。

相关内容