18

Is it always true that when a new file is created in a directory - the directory's mtime changes?

kolypto
  • 10,738
  • 12
  • 51
  • 66

1 Answers1

18

The short answer is yes, it should. A longer answer follows.

A directory is a special kind of file; its contents is a set of (filename, i-node) tuples. Thus directory's mtime is updated whenever a tuple is added, removed or changed - that is, a file of any type (including hard links and directories) is added, removed or renamed in the directory (but not in nested directories - those are different "files").

Please note when file's metadata changes (owner, group, mode, atime, ctime, mtime - hosted in i-node), mtime is NOT updated (neither on the file nor containing dir) but the file's ctime does.

See also Base Definitions volume of IEEE Std 1003.1-2001, Section 4.7, File Times Update.

yrk
  • 2,347
  • 16
  • 22
  • 1
    Note, that this is only true for files that are created (etc) directly inside of the parent directory in question, but not for files created (etc) in a directory under the parent directory. Perhaps this is obvious to others, but it wasn't to me (I had to test to find the answer). – logidelic May 03 '18 at 20:58
  • 1
    @logidelic thanks, I've updated the answer to make it clearer. – yrk May 06 '18 at 10:10
  • Why does the directory's ctime also change when you create a new file in it? – Mr. Roland Feb 24 '20 at 08:57
  • @Mr.Roland Because ctime changes whenever any status changes, and mtime is one of the statuses. So mtime change causes ctime change. – aafulei Dec 17 '20 at 03:55