Technical skill

Linux command: stat

Find details a folder listing leaves out

A file has a name and contents, but the operating system stores other details about it too. Those details are called metadata: they include the file type, size, owner, permissions, and timestamps. A directory listing may show only some of them. The stat command prints metadata for a path; it does not read the file's contents. Try stat report.txt to see its default report.

On systems with GNU stat, a format string lets you choose fields:

stat -c '%n: %F, %s bytes, mode %a, owner %U:%G, modified %y' report.txt

This prints the path, file type, logical size in bytes, permission bits in octal notation, user and group names, and modification time. Octal is a way of writing permission bits as numbers such as 644. GNU's format directives are useful in scripts, but -c is a GNU feature rather than a portable POSIX interface. If a command must work on different Unix-like systems, check each system's manual.

Check a link, then check its target

A symbolic link is a file that points to another path. With GNU stat, stat linkname reports the link itself; stat -L linkname follows it and reports the target. The link and the destination are separate file-system objects and can have different owners, permissions, and timestamps. If a link points to a path that no longer exists, it is dangling: you can inspect the link, but following it fails.

Read sizes and timestamps correctly

The size printed by stat is the file's logical length, not necessarily the storage it uses on disk. A sparse file has gaps that do not occupy data blocks; compression and deduplication can also make physical use differ. Compare stat with du or file-system tools when measuring storage consumption.

Modification time (mtime) changes when file data changes. Status-change time (ctime) records a metadata change such as an owner or permission update; it does not mean creation time. Some file systems expose a birth time, but it may be unavailable. Access time (atime) follows mount options and file-system policy, so it may not change on every read.

GNU stat -f report.txt describes the file system, including its type and block counts, rather than the file itself. df answers a related question by summarizing used and available space on mounted file systems.

When a path behaves unexpectedly, inspect both the link and target, then check permissions on every directory along the path. stat config-link, stat -L config-link, and namei -l config-link (where available) show the link, target, and path components. Treat the output as a snapshot: a file can change before the next command, and metadata alone does not prove that a particular process can open it under its credentials, mount namespace, or security policy.

Interactive exercises

Materials

Skillsets
Skillset Linux for backend developer preview
image descriptionLinux for backend developer
by Artem Naumenko (fb)
16 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Skyeng SRE preview
image descriptionSkyeng SRE
by Yuliya Marochkina
12 users
Skillset Skyeng Senior PlatOps preview
image descriptionSkyeng Senior PlatOps
by Artem Naumenko (slack2)
7 users
Skillset Skyeng linux system administrator preview
image descriptionSkyeng linux system administrator
by Vitaliy Okulov
3 users
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Skillset Skyeng Ops (for DevOps) preview
image descriptionSkyeng Ops (for DevOps)
by Roman Burnashev
2 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful