7

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

1 Answer 1

10

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).

6
  • you mean cat to the file, I think. (with output redirection) Aug 11, 2015 at 18:21
  • @PeterCordes well, I guess it depends on what the goal is, if you want to change it, then cat to the file would do so without modifying the directory times, but if he just wanted to look at the file or "use" the file without changing them he could cat it. Rereading the question though, I think your correction is probably closer to what the OP asked. Aug 11, 2015 at 18:41
  • 1
    My point was that reading a file with cat foo.txt doesn't change any timestamps (except potentially atime), and is completely irrelevant to this question. Loading it in an editor also wouldn't change it. (They don't create autosave files until you make a change.) Aug 11, 2015 at 18:44
  • 1
    OK, I see why mtime would change (because the directory listing is changing). But what about ctime? I was under the impression ctime is for inode information like permissions, owners, groups, etc...Thanks. Aug 12, 2015 at 0:29
  • @MathStudent the inode will also change when a new file is created or a file is deleted or created, since by that inode information is not the same as before (which was nonexistent). So creating a file or deleting a file will update both mtime and ctime since the inode list is changing. Changing the permissions of the directory itself will also change the ctime (but not changing the permissions of a file inside the directory) Aug 12, 2015 at 0:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .