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

  • This is quite easy to test... – ewwhite May 10 '12 at 20:08
  • 6
    Easy. However, not on all OSes & filesystems at once :) – kolypto May 10 '12 at 20:12

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.

  • 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 3 '18 at 20:58
  • 1
    @logidelic thanks, I've updated the answer to make it clearer. – yrk May 6 '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 8: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 3:55

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.