6

How does change of atime, mtime or ctime of a file affect

  • the atime, mtime or ctime of its parent directory and
  • the atime, mtime or ctime of its ancestral directories?
Anthon
  • 79,293
Tim
  • 101,790

2 Answers2

6

You can easily get that information using stat. As for ancestral directories, it is easily checked that if a file changes that this doesn't affect anything "up the hierarchy" by looking at /:

root@pooh:/home/anthon-mint# stat /
  File: ‘/’
  Size: 4096        Blocks: 8          IO Block: 4096   directory
Device: 804h/2052d  Inode: 2           Links: 30
Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2015-02-22 09:57:14.028146463 +0100
Modify: 2015-01-01 10:34:05.528461374 +0100
Change: 2015-01-01 10:34:05.528461374 +0100
 Birth: -

as the system is constantly changing files, these values should be close to the current time.

If you create a new directory, and then a file in it, access and modification time of the directory change:

$ mkdir tmp
$ stat tmp
  File: ‘tmp’
  Size: 4096        Blocks: 8          IO Block: 4096   directory
Device: 700h/1792d  Inode: 144141      Links: 2
Access: (0775/drwxrwxr-x)  Uid: ( 1001/  anthon)   Gid: (  100/   users)
Access: 2015-02-27 16:19:02.523585508 +0100
Modify: 2015-02-27 16:19:02.523585508 +0100
Change: 2015-02-27 16:19:02.523585508 +0100
 Birth: -
$ touch tmp/bla
$ stat tmp
  File: ‘tmp’
  Size: 4096        Blocks: 8          IO Block: 4096   directory
Device: 700h/1792d  Inode: 144141      Links: 2
Access: (0775/drwxrwxr-x)  Uid: ( 1001/  anthon)   Gid: (  100/   users)
Access: 2015-02-27 16:19:02.523585508 +0100
Modify: 2015-02-27 16:19:18.639585445 +0100
Change: 2015-02-27 16:19:18.639585445 +0100
 Birth: -

Access time doesn't change, but the creation of the new file changes modification and change time.

Now touch the file again:

$ touch tmp/bla
$ stat tmp
  File: ‘tmp’
  Size: 4096        Blocks: 8          IO Block: 4096   directory
Device: 700h/1792d  Inode: 144141      Links: 2
Access: (0775/drwxrwxr-x)  Uid: ( 1001/  anthon)   Gid: (  100/   users)
Access: 2015-02-27 16:19:02.523585508 +0100
Modify: 2015-02-27 16:19:18.639585445 +0100
Change: 2015-02-27 16:19:18.639585445 +0100
 Birth: -
$ 

And the directory doesn't change, but none of the information for the directory changes, as no new file is created.

Changing the mtime, atime, or ctime of an existing file has no effect on the directory it is in, nor on any of that directory's parents.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Anthon
  • 79,293
4

At most the atime (access time) field of the directory will be updated, if the file's inode was not already in the cache. However the default with modern Linux kernels is to mount filesystem with the relatime flag, meaning that the atime is only updated if the file or directory is accessed after the modification time (mtime) and the current atime is earlier than the mtime.

The mtime (modification time) will only be updated if the directory is actually modified, e..g. a file (or directory, or other entry) is deleted, renamed, or added, or the directory is renamed (moved).

The ctime (inode change time) is modified whenever anything in the directory's inode is changed, including permissions, owener, and also mtime...

wurtel
  • 16,115
  • "The ctime (inode change time) is modified whenever anything in the directory's inode is changed, including permissions, owner, and also mtime..." Can you provide link to some doc? I thought ctime would only change when metadata of the directory e.g. permissions are changed. – Roland Feb 25 '20 at 07:12
  • @Roland All the things I name are metadata: permissions, owner, mtime. – wurtel May 23 '20 at 11:57