1

i would like to be able to chmod a directory structure without changing the atime of those (files and) directories.

if i start out with a file structure like

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

and list the atimes of the files and directories

$ 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  

and then chmod those with

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

this changes the atimes of the directories (and yes, for good reasons); the mtimes remain unaffected:

$ 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  

is there a simple way to avoid that? i could of course write a script, that stores the atime before the modification and then resets it afterwards. but is there a simpler way?

2

In your example only the directory was changed. You can disable atime by adding nodiratime (only directories) or noatime (files and directories, includes nodiratime) to your mount options in /etc/fstab. Only ctime should change then.

The kernel default is relatime unless overridden and is displayed when you run the mount command.

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.

Btw. your chmod doesn't work here. Did you mean chmod -R u=rwx,g=rwx,o=rwx test?

  • thanks. good idea. only i have no access to fstab. and you are right: only the directory changed! i must have been blind! (cleaned that up in the question a bit) – hiro protagonist Feb 28 at 12:36
  • oh, thought i had fixed the mode for chmod (that was from a first test...). thanks! – hiro protagonist Feb 28 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.