chmod 而不更改文件的 stat (atime)

chmod 而不更改文件的 stat (atime)

我希望能够在不更改这些(文件和)目录的情况下获得chmod目录结构。atime

如果我从像这样的文件结构开始

$ tree test/
test/
├── test1.txt
└── text2.txt

并列出atime文件和目录的 s

$ find test/ -exec stat --printf='name: %n atime: %x\n' {} \;
name: test/          atime: 2019-02-28 11:28:24.418369586 +0100  
name: test/text2.txt atime: 2019-02-28 11:28:03.609919183 +0100  
name: test/test1.txt atime: 2019-02-28 11:27:58.101799544 +0100  

然后是chmod那些

$ chmod -R 'u=Xrw,g=Xrw,o=Xr' test

这会更改atime目录的 s (是的,有充分的理由); smtime不受影响:

$ find test/ -exec stat --printf='name: %n atime: %x\n' {} \;
name: test/          atime: 2019-02-28 11:38:30.590740343 +0100  
name: test/text2.txt atime: 2019-02-28 11:28:03.609919183 +0100  
name: test/test1.txt atime: 2019-02-28 11:27:58.101799544 +0100  

有没有一种简单的方法可以避免这种情况?我当然可以编写一个脚本,存储atime修改前的内容,然后在修改后重置它。但有没有更简单的方法呢?

答案1

在您的示例中,仅更改了目录。您可以通过将nodiratime(仅目录)或noatime(文件和目录,包括nodiratime)添加到 中的挂载选项来禁用 atime /etc/fstab。那么只有 ctime 应该改变。

除非被覆盖,否则内核默认值relatime将在您运行命令时显示mount

安装 (8)

 relatime
              Update  inode  access  times relative to modify or change time.  Access time is only
              updated if the previous access time was earlier than the current  modify  or  change
              time.   (Similar  to  noatime,  but it doesn't break mutt or other applications that
              need to know if a file has been read since the last time it was modified.)

              Since Linux 2.6.30, the kernel defaults to the  behavior  provided  by  this  option
              (unless  noatime  was  specified),  and the strictatime option is required to obtain
              traditional semantics.  In addition, since Linux 2.6.30, the file's last access time
              is always updated if it is more than 1 day old.

顺便提一句。你的 chmod 在这里不起作用。你的意思chmod -R u=rwx,g=rwx,o=rwx test

相关内容