Why are mtime and ctime changing for a directory when a file is modified

directoryeditorsfilestimestamps

I am learning about atime, ctime, mtime with regard to files and directories. It seems to me that if I modify a file within a directory, I haven't changed the "directory file" itself in its inode or file contents, and therefore ctime and mtime should be unchanged.

But in the following test, the change time and modification time did change when I edited the file. Why did they change?

$ ls    

blah.txt  test.txt  test.txt~

$ cd ..

$ stat -x Write
      File: "Write"
      Size: 170          FileType: Directory
      Mode: (0777/drwxrwxrwx)         Uid: (  501/user)  Gid: (   20/   staff)
    Device: 1,4   Inode: 652017    Links: 5
    Access: Tue Aug 11 08:20:33 2015
    Modify: Tue Aug 11 08:01:49 2015
    Change: Tue Aug 11 08:01:49 2015

$ cd Write

$ ls
    blah.txt  test.txt  test.txt~

$ emacs test.txt

$ cd ..

$ stat -x Write
      File: "Write"
      Size: 170          FileType: Directory
      Mode: (0777/drwxrwxrwx)         Uid: (  501/user)  Gid: (   20/   staff)
    Device: 1,4   Inode: 652017    Links: 5
    Access: Tue Aug 11 08:20:48 2015
    Modify: Tue Aug 11 08:20:48 2015
    Change: Tue Aug 11 08:20:48 2015

Best Answer

When you run emacs it creates a backup file, int his case test.txt~. If there was already a file with that name I suspect it deletes it and creates a new one. That new file creation is modifying the directory, and thus updating its modified and changed times.

If you were, instead, to say echo new line >> blah.txt you would not be creating any extra files, and so would not update those entries on the directory. In this case, the shell just opens the file (for append).