30 April 2021

The stat utility prints information about files. You are mostly likely to use it when you want to know when a file was last changed, modified or accessed, but you can also use it to print specific bits of information about files.

ctime, mtime and atime

Every file on a Unix system has three timestamps:

  • ctime is the date a file was last changed. A “change” can be either a content change (i.e. a file edit) or a meta data change. For instance, when you change the ownership of permissions of a file the ctime is updated.
  • mtime is the date a file was modified – that is, when the content of the file changed.
  • atime is the date a file was last accessed. This includes the file being accessed by utilities such as grep. This can obviously cause a lot of writes, and most file systems are therefore mounted with the relatime option. This option only updates the atime if the file is accessed and the timestamp is older than the ctime and mtime.

There is a fourth time stamp: birth stores the date a file was created. This option is not used (the general consensus is that the time stamp is of no value).

Viewing the timestamps

By default, ls -l shows a file’s mtime. You can get ls to print the ctime or atime instead (it has a --time= option). However, it is much easier to use stat instead, as the utility’s output includes all three timestamps:

$ stat config.php
  File: config.php
  Size: 3618      	Blocks: 8          IO Block: 4096   regular file
Device: fd02h/64770d	Inode: 33599891    Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/example)   Gid: ( 1000/example)
Context: unconfined_u:object_r:httpd_sys_content_t:s0
Access: 2020-11-13 20:48:42.270076603 +0000
Modify: 2020-07-29 10:35:28.000000000 +0100
Change: 2020-11-09 11:46:48.941657799 +0000
 Birth: -

Printing specific information

You can use the --format option to print specific bits of information. This is mainly useful in scripts. For instance, we use a script that finds large files. The script then loops through the list with files and uses stat to show the file size, date and name.

There is lots of other information stat can print. As a random example, let’s say you want to know the file name and the time any of the time stamps last changed. You can get that information with --format="%n %z" (%n prints the file name and %z is the time of the last status change):

$ stat --format="%n %z" config.php
config.php 2020-11-09 11:46:48.941657799 +0000

You can “prettify” the output by using --printf rather than --format. The below command prints the file size, name, last status change and file type in columns:

$ stat --printf="%s\t%n\t%z\t%F\n" *php
3618	config.php	2020-11-09 11:46:48.941657799 +0000	regular file
19576	feed.php	2020-11-09 11:46:48.942657802 +0000	regular file
2540	index.php	2020-11-09 11:46:48.942657802 +0000	regular file
20463	install.php	2020-11-09 11:46:48.942657802 +0000	regular file

There are lots more options – you can find them all by running man 1 stat.