Understanding Unix Filesystem Timestamps by asicsp
Published on 2022-10-25.
The POSIX standard requires that operating systems maintain filesystem metadata that record when each file was last accessed. Let’s take a look at what all that means.
A lot of information on the Internet about filesystem timestamps are either outdated or misunderstood. Some people also tend to generalize a specific option’s pros and cons across all setups, not knowing when the option is actually appropriate. For example, I might think that setting atime
to on
is unconditionally bad because it will have a detrimental effect on the performance of the filesystem without knowing that atime
is required by some applications in order to function properly.
The POSIX standard mandates that metadata for three timestamps are stored in the filesystem regarding each file. These three timestamps are:
- atime – the access timestamp of the last read.
- mtime – the modification timestamp of the last write.
- ctime – the status change timestamp of the last change to the file’s meta-information.
An example of a change to the ctime
property is when the file permissions change. Changing file permissions doesn’t access the file itself, so atime
doesn’t change. The file is also not modified in any way, so the mtime
options also doesn’t change. Still, we have changed the permissions of the file itself, and this must be registered somewhere. The POSIX standard gives this job to the ctime
field.
But why would we need to store that information in the first place? Well, if we make a backup of the file, a backup tool may need to know if the file permissions has changed in order to determine if a new backup is required, or if it can simply skip the file because nothing has changed. Another operation that also changes the ctime
option is renaming a file.
As you can properly guess, the atime
property comes with both a performance penalty and a wear penalty on a very busy filesystem because every read operation on a file also generates a write operation. When you read the file, the atime
metadata field is updated, which requires a write.
So, a file’s atime
, mtime
, and ctime
are set to the current time whenever you read, write, or change the attributes of the file. Searching a directory also counts as reading it. And a file’s atime
and mtime
can be set directly, via the touch command.
On any UNIX-like operating system, like Linux or BSD, you can display a file’s timestamps with the stat
command.
On Arch Linux:
$ stat /bin/ls File: /bin/ls Size: 137744 Blocks: 272 IO Block: 4096 regular file Device: 254,0 Inode: 29897362 Links: 1 Access: (0755/-rwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2022-04-21 22:11:19.000000000 +0200 Modify: 2022-04-17 20:21:13.000000000 +0200 Change: 2022-04-21 22:11:19.55507