The stat command in Linux: view detailed file information

Introduction

In the Linux environment, knowing the details of a file goes far beyond its name or size. The stat command allows you to obtain a complete view of the metadata associated with any file or directory, from permissions and timestamps to the inode number and the filesystem block where it resides. This tool is essential for system administrators, developers, and any user who needs to perform audits, debug permission issues, or simply understand how the system manages storage.

Basic syntax

The simplest way to use stat is:

stat [opciones] archivo

If no option is specified, the command shows a default output that includes:

  • File name
  • Size in bytes
  • Allocated blocks
  • File type (regular, directory, link, etc.)
  • Permissions in octal and symbolic format
  • Last access, modification, and change dates
  • Inode number and number of hard links

This default output is useful for a quick inspection, but the true power of stat is revealed when you use its format options.

More useful options

The true power of stat lies in its format options. With the -c or --format option you can define exactly which fields you want to see and in what order.

  • -c FORMAT : specifies a format string using sequences such as %n (name), %s (size), %U (owner), %G (group), %a (permissions in octal), %A (symbolic permissions), %X (last access time in seconds since epoch), %Y (last modification time), %Z (last change time).
  • -f : shows information about the filesystem where the file resides, instead of the file itself.
  • -L : follows symbolic links and shows the information of the target file.
  • -t : displays the output in terse form, useful for scripts that require a compact format.

Furthermore, you can combine several options to obtain more specific information. For example, stat -Lc '%n %s %U' will show the name, size, and owner of the file pointed

This post is also available in ESPAÑOL.

Esta obra está bajo una Licencia Creative Commons Atribución 4.0 Internacional para Francesc Roig francesc@vivaldi.net .