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?
How to Change Only the mtime of a Directory?
filestimestamps
Related Solutions
You can easily get that information using stat
. As for ancestral directories, it is easily checked that if a file changes that this doesn't affect anything "up the hierarchy" by looking at /
:
root@pooh:/home/anthon-mint# stat /
File: ‘/’
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 804h/2052d Inode: 2 Links: 30
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2015-02-22 09:57:14.028146463 +0100
Modify: 2015-01-01 10:34:05.528461374 +0100
Change: 2015-01-01 10:34:05.528461374 +0100
Birth: -
as the system is constantly changing files, these values should be close to the current time.
If you create a new directory, and then a file in it, access and modification time of the directory change:
$ mkdir tmp
$ stat tmp
File: ‘tmp’
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 700h/1792d Inode: 144141 Links: 2
Access: (0775/drwxrwxr-x) Uid: ( 1001/ anthon) Gid: ( 100/ users)
Access: 2015-02-27 16:19:02.523585508 +0100
Modify: 2015-02-27 16:19:02.523585508 +0100
Change: 2015-02-27 16:19:02.523585508 +0100
Birth: -
$ touch tmp/bla
$ stat tmp
File: ‘tmp’
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 700h/1792d Inode: 144141 Links: 2
Access: (0775/drwxrwxr-x) Uid: ( 1001/ anthon) Gid: ( 100/ users)
Access: 2015-02-27 16:19:02.523585508 +0100
Modify: 2015-02-27 16:19:18.639585445 +0100
Change: 2015-02-27 16:19:18.639585445 +0100
Birth: -
Access time doesn't change, but the creation of the new file changes modification and change time.
Now touch the file again:
$ touch tmp/bla
$ stat tmp
File: ‘tmp’
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 700h/1792d Inode: 144141 Links: 2
Access: (0775/drwxrwxr-x) Uid: ( 1001/ anthon) Gid: ( 100/ users)
Access: 2015-02-27 16:19:02.523585508 +0100
Modify: 2015-02-27 16:19:18.639585445 +0100
Change: 2015-02-27 16:19:18.639585445 +0100
Birth: -
$
And the directory doesn't change, but none of the information for the directory changes, as no new file is created.
Changing the mtime, atime, or ctime of an existing file has no effect on the directory it is in, nor on any of that directory's parents.
As @Celada said, this is really easy to test.
One note however: ctime
is the last time where inode informations have been changed (the inode number doesn't change).
So:
- If a file's content is stored outside its inode, changing its content will change the mtime to the present, but will that changes its ctime and atime to the present?
If you change content, the mtime
will change but also the ctime
because the timestamps of the file (and maybe its size) are updated and those informations are store in inode.
Plus, say this is a text file and you modify its content using vi
, so the atime
will also be updated because obviously vi
will read the file before displaying it.
Example:
$ touch file1
$ stat file1
Access: 2015-05-06 19:11:41.887622158 +0200
Modify: 2015-05-06 19:11:41.887622158 +0200
Change: 2015-05-06 19:11:41.887622158 +0200
$ echo "hello" >> file1
$ stat file1
Access: 2015-05-06 19:11:41.887622158 +0200
Modify: 2015-05-06 19:12:27.816047883 +0200
Change: 2015-05-06 19:12:27.816047883 +0200
- If we change a file's attributes stored in its inode, that will change ctime to present, but will that change mtime and atime to present?
If you change only file's attribute, only the informations stored in inode change, so yes, only ctime
will change.
$ stat file2
Access: 2015-05-06 19:28:09.378880724 +0200
Modify: 2015-05-06 19:28:09.378880724 +0200
Change: 2015-05-06 19:28:09.378880724 +0200
$ chmod 700 file2
$ stat file2
Access: 2015-05-06 19:28:09.378880724 +0200
Modify: 2015-05-06 19:28:09.378880724 +0200
Change: 2015-05-06 19:30:24.679022346 +0200
- Does changing anything related to a file such as its content, attributes, or inode, also change atime?
As I said for the first question, if you don't need to read the file to change those informations, no, atime
won't change.
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.