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.
Think of a directory as a text file that looks something like:
123:foo.txt
234:bar.txt
123:other-name-for-foo.txt
That's all there is (at least in traditional Unix file systems).
foo.txt
is not a child of that directory in that it's not an exclusive relationship. It's just that the file of inode number 123 is referenced in two entries of that directory: with names foo.txt
and other-name-for-foo.txt
.
Those two directory entries are also known as two hard links to the same file.
$ touch foo.txt bar.txt
$ mkdir other-dir
$ ln foo.txt other-name-for-foo.txt
$ ln foo.txt other-dir/whatever
$ ls -i . other-dir
.:
58892735 bar.txt 58958143 other-dir/
58879562 foo.txt 58879562 other-name-for-foo.txt
other-dir:
58879562 whatever
.
and other-dir
are two files of type directory, each containing a number of directory entries. ls
lists the entries of a directory whose names don't start with .
and with -i
includes the inode number.
With -a
(to include dotfiles), and -i
(ls -ai
), that's more or less dumping the content of that text file I was mentioning above.
You'll notice that the file of inode 58879562 above is linked once to other-dir
and twice to .
. That 58879562 file now has a link-count of 3, but neither .
nor other-dir
care about that. That information is stored in the inode of the file and is just for the system to keep track of how many times the file is linked to a directory. If that number falls to 0, the disk space occupied by that file can be reclaimed.
In addition to inode number and name, directories also contain the file type of the file (regular/fifo/directory/symlink/device..., though not for all file systems). That information can be retrieved otherwise (as it's stored in the inode) so is not necessary. It is done mainly for optimisation purposes and because it's possible. It is possible because it is not possible to change the type of a file (contrary to other metadata), so that information stored in the directory is not going to become out-of-sync with the reality.
You'll also notice that the system call to remove a file is not remove("/foo/bar")
but unlink("/foo/bar")
. That's because all it does is remove the bar
entry from the /foo
directory, it unlinks bar
from /foo
. That file may still be linked to other (or the same by a different name) directory. It's only going to be removed when its link count reaches zero (when it's no longer linked to any directory).
As to why doing an ls
doesn't update the access time of a directory, that's probably because your filesystem is mounted with the noatime
or relatime
option.
Updating the access-time of a file (directory or other) every time it is read is costly, so many systems give up on that nowadays.
Note that the system, if not the user reads directories all the time.
For instance, when accessing /foo/bar
, the system needs to read the content of /
to find foo
and then /foo
to find bar
(though most of the time that information is cached). However, in those cases, the atime is not updated (thankfully), only when user-space processes explicitely read the content of a directory like ls
does.
As to when the ctime
of a directory is updated, that's the same as for a regular file: when its content is modified (entries added or removed or renamed) or its metadata (except atime
upon reading) is modified (like upon chmod
/chown
, ACL modifications...).
Best Answer
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 you change content, the
mtime
will change but also thectime
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 theatime
will also be updated because obviouslyvi
will read the file before displaying it.Example:
If you change only file's attribute, only the informations stored in inode change, so yes, only
ctime
will change.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.