How to Use the Linux stat Command to View Detailed File Information

In Linux, every file and directory has a wealth of metadata attached to it beyond just its name and contents. This metadata includes the exact file size in bytes, the number of hard links pointing to it, the inode number, the file permissions in both symbolic and octal form, and three separate timestamps tracking when the file was last accessed, modified, and had its status changed.

While the ls -l command shows some of this information, it omits critical details like the inode number, the exact byte count, and the birth timestamp. The stat command is the dedicated tool for displaying the complete, unabridged metadata profile of any file or directory in your Linux filesystem.

Basic Usage of the stat Command

To view the full metadata for a file, simply pass the filename as an argument.

Open your terminal and type:

stat myfile.txt

The output will display a comprehensive block of information. Here is what each field means:

  • File: The name of the file you queried.
  • Size: The exact size of the file in bytes. Unlike ls -lh which rounds to kilobytes or megabytes, stat gives you the precise byte count.
  • Blocks: The number of 512-byte blocks allocated on disk. Due to how filesystems allocate storage, a small file might occupy more blocks than its actual size would suggest.
  • IO Block: The optimal block size for filesystem I/O operations (typically 4096 bytes on modern ext4 filesystems).
  • Device: The identifier of the storage device where the file physically resides.
  • Inode: The unique inode number assigned to the file by the filesystem. This is the file’s true identity at the kernel level; filenames are just human-friendly labels pointing to inodes.
  • Links: The number of hard links pointing to this inode. A regular file with no additional hard links will show 1.
  • Access (permissions): The file permissions displayed in both octal notation (e.g., 0644) and symbolic notation (e.g., -rw-r--r--).
  • Uid / Gid: The user ID and group ID of the file’s owner, along with the corresponding usernames.

Understanding the Three Timestamps

One of the most valuable features of the stat command is that it displays all three (or four) timestamps associated with a file. The standard ls -l command only shows the modification time by default.

  • Access (atime): The last time the file’s contents were read. This updates when you use commands like cat, less, or when a program opens the file for reading.
  • Modify (mtime): The last time the file’s actual data content was changed. This is the timestamp that ls -l displays.
  • Change (ctime): The last time the file’s metadata (permissions, ownership, or hard links) was altered, even if the content itself was not modified. Renaming a file or running chmod on it will update this timestamp.
  • Birth: The original creation time of the file. Note that not all Linux filesystems support the birth timestamp. On older ext3 or XFS filesystems, this field may display a hyphen (-), indicating the data is unavailable.

Using stat on Directories

The stat command works identically on directories. Running stat /home/username will display the directory’s permissions, ownership, inode, and timestamps, just as it would for a file.

One useful detail for directories is the “Links” count. For a directory, the link count represents the number of subdirectories it contains plus two (one for the directory’s own entry . and one for the parent directory entry ..). This gives you a quick way to count immediate subdirectories without running ls.

Customising the Output Format

If you only need a specific piece of information (for example, just the file size or just the inode number), you can use the --format or -c flag with format tokens to extract exactly what you need.

Common Format Tokens

  • %s — File size in bytes
  • %i — Inode number
  • %a — Permissions in octal
  • %U — Owner username
  • %G — Group name
  • %y — Last modification time (human-readable)
  • %n — File name

For example, to print only the size and name of a file in a clean, scriptable format:

stat -c '%s %n' myfile.txt

Output: 4096 myfile.txt

This custom formatting is particularly powerful when combined with shell scripts and loops, allowing you to process file metadata in bulk.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.