$_ CLI In Depth
Learning progress 0%
lesson filesystem inodes navigation

stat, Timestamps & File Metadata

5 min read Module 2: Filesystem Navigation & Inodes Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:11

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.

BASH4 lines
cd /workspace
echo "twelve bytes" > sample.txt
stat sample.txt
stat -c 'size=%s mode=%a type=%F' sample.txt

Useful 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

TimestampChanges whenShown by
Access (atime)The file contents are readstat -c %x, ls -lu
Modification (mtime)The file contents are written or truncatedstat -c %y, default ls -l
Change (ctime)The inode changes: permissions, owner, link count, or contentsstat -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.

BASH3 lines
cd /workspace
touch -d @1000000000 sample.txt
stat -c '%Y %n' sample.txt

The 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.

Note

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.

Visualizer

A directory is a list of (name, inode number) pairs. The inode holds the file itself. Hard links add names; symlinks store a path.

1 / 5
directory /workspace
nameinode
a1201
inode table
inodetypelinksdata
1201regular1"hi\n"

echo hi > a allocates inode 1201 and adds the entry a → 1201 to the directory. The link count is 1.

cd /workspace; echo hi > a; ln a b; ls -li a b; rm a; cat b; stat -c "%h links" b
Terminal lab · 3 steps · real Linux in your browser

Read metadata with stat

Read a file size, identify a directory with a format string, and set an exact epoch time.

01
Read the size in bytes
Save only the size of meta/data.txt in bytes: ``bash stat -c %s meta/data.txt > size.txt ``
Show hint for step 1
The format directive %s prints the size in bytes.
02
Identify the type
Save the file type of the meta directory as a word: ``bash stat -c %F meta > type.txt ``
Show hint for step 2
The directive %F prints the type, such as regular file or directory.
03
Set and read an exact time
Set the modification time of meta/data.txt to the epoch second 1000000000, then save that value: ``bash touch -d @1000000000 meta/data.txt stat -c %Y meta/data.txt > epoch.txt ``
Show hint for step 3
The at sign in front of a number tells touch -d to read seconds since the epoch.
Check yourself Question 1 of 4
Concept Check Change time recall

Which timestamp changes when only the permissions of a file are changed?

Recap & Key Takeaways

stat prints the full inode metadata, and -c takes a format string for the fields you need.
mtime changes with the data, ctime changes with any inode change, and atime records reads but is updated lazily.
touch -d @SECONDS sets an exact time, and epoch seconds are the safest format for scripts.
The change time is not the creation time, and birth time is not available on every filesystem.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue