What stat reports
The command stat asks the kernel for the attributes of an inode and prints them. With no options it shows the file name, size, number of blocks, I/O block size, device and inode numbers, link count, the access rights in octal and symbolic form, the owner and group, and the three timestamps. The output is long because it is complete. For scripts, the -c option takes a format string, so you can request exactly the fields you need.
cd /workspace
echo "twelve bytes" > sample.txt
stat sample.txt
stat -c 'size=%s mode=%a type=%F' sample.txtUseful format directives include %s for size in bytes, %a for permissions in octal, %A for them in symbolic form, %F for the file type as a word, %i for the inode number, %h for the link count, and %y or %Y for the modification time in readable or epoch form.
The three timestamps
| Timestamp | Changes when | Shown by |
|---|---|---|
| Access (atime) | The file contents are read | stat -c %x, ls -lu |
| Modification (mtime) | The file contents are written or truncated | stat -c %y, default ls -l |
| Change (ctime) | The inode changes: permissions, owner, link count, or contents | stat -c %z, ls -lc |
The change time is the one people misread. It is not the time the file was created, and it is not the time of the last edit to a name. Chmod, chown, and a new hard link all update it, and so does writing to the data. A file can have an old modification time and a recent change time after someone changed its permissions. The kernel also has a birth time on some filesystems, which stat reports as %w when it is available. Do not depend on it, because many tools and filesystems do not record it.
Why access time is usually lazy
Updating the access time on every read would require a disk write for every read, which is expensive. Most Linux filesystems mount with the option relatime, which updates the access time only when it is older than the modification time or more than a day old. The result is that atime is a rough signal, not a precise log of reads. Do not use it to decide whether a file is safe to delete.
A common question is whether a time can be trusted at all. The answer depends on who wrote it. The kernel sets the timestamps itself, using its own clock, so they are reliable for ordinary work on one machine. A program can ask the kernel to set a modification time to an arbitrary value, which is what touch does, so a timestamp copied from an archive or set by a tool is only as trustworthy as the tool. Forensic work and build systems therefore look at several fields and at the source of each value, rather than at one number.
Setting times with touch
The command touch updates the access and modification times of a file, and it creates the file if it does not exist. The option -d sets an exact time, and an at-sign followed by a number of seconds since the Unix epoch is accepted by GNU touch. This is useful for building test files with known dates. Changing the times with touch also updates the change time, because the inode has been modified.
cd /workspace
touch -d @1000000000 sample.txt
stat -c '%Y %n' sample.txtThe output shows the epoch value, which is independent of your time zone. Epoch seconds are the best format for comparing times in scripts, because they are plain integers.
Which operations update which time
It helps to walk through common operations and note which timestamps they touch. Reading a file with cat normally updates only the access time, and only under the lazy rule described above. Writing to a file updates the modification time and, because the inode changed, the change time as well. Changing permissions with chmod updates only the change time, since the contents are untouched. Renaming a file updates the change time of the file and of the directory that holds it, because the directory contents changed.
Copying with cp gives the new file the current time for all three timestamps, unless you ask it to preserve the originals with -p. Note that -p preserves the modification time and the access time, but the change time always reflects the moment of the copy, because the kernel records that the new inode was created. A backup that must keep the original change time cannot be made with an ordinary copy, which is one reason backup tools work at a lower level.
When you debug a build, the modification time usually answers "did the source change" and the change time answers "did anything about this file change". Using the right one prevents false alarms, such as a rebuild triggered by a permission change that does not affect the output.
Comparing timestamps in practice
The command find from a later module can select files by time with options such as -newer and -mtime. For a single file, comparing two epoch values with stat is enough. The most common need is to know whether one file changed after another was built. Compare modification times for content and change times for metadata, and remember that copying a file with cp without preserve options gives the copy a new modification time.
The times shown by stat are in the timezone of the process, and the format of the default output includes fractional seconds on Linux. For scripts, prefer %Y and compare integers. Avoid parsing the default text.
Practice in the lab
The lab asks you to record the size of a file, the type of a directory, and an epoch timestamp that you set yourself. Each value goes into its own file in /workspace so that the checker can compare it exactly.