How to Change Only the mtime of a Directory?

filestimestamps

I am trying to change just the mtime of a directory by adding files or sub directories but that changes the ctime of the file too. Is there any way to just change the mtime without affecting the ctime or atime?

Best Answer

It's not possible because ctime is changed always if there is a change of mtime. Here is the explanation:

Let's see from file perspective:

ctime is the inode/file change time, it means that ctime is updated when the file attributes are changed, like changing the owner or the permission.

mtime is the file modify time, it's updated when you modify the content of a file.

Most of the times ctime and mtime will be the same, unless only the file attributes are updated. In that case only the ctime gets updated.

Now from a directory perspective:

A directory is a special kind of file and its content is a set of (filename, i-node) tuples. The mtime of the directory is updated when a tuple is added, removed or changed, it means when a file or directory is added, deleted or renamed, mtime changes and it triggers a change of ctime.

When the metadata of the directory changes (owner, group, mode), mtime is NOT updated but ctime does.