The Linux Stat command: see detailed file information

Introduction

In the day-to-day of a system manager or developer working in Linux environments, knowing the metadata of a file is as important as knowing its content. While commands likelsorfindgive us an overview, commandstatallows us to deepen and extract detailed information that often goes unnoticed. In this article we will explore what isstat, how your syntax works, what options it offers and how to interpret each of the fields it returns.

What is stat?

The commandstatbelongs to the GNU choreutile basic utility package and is in charge of showing the status of a file or file system. Unlikels -lshowing a legible but limited format,statpresents all the attributes that the kernel maintains in the odor: permissions, number of links, owner, group, size, assigned blocks, and the various timstamps (access, modification and change of status). In addition, it allows the user to define a custom output format using the option-cor--format, which makes it a very flexible tool for scripts and automation.

Basic syntax

The simplest way to usestatis:

stat [options] file

If no option is specified, the command shows a default output divided into blocks: file information, the file system where it resides and, optionally, any symbolic link that is followed. Some of the most common options are:

  • -Lor--dereference: follows symbolic links and shows the information of the file to which they point.
  • -for--file-system: shows file system statistics instead of file.
  • -c FORMATor--format=FORMAT: defines a custom output format using exhaust sequences.
  • -tor--terse: compact output, useful for later processing.

Most useful format options

The real power ofstatis in its ability to format the output. Some of the most used exhaust sequences are listed below:

  • %n: file name.
  • %s: total size in bytes.
  • %U: name of owner.
  • %G: name of the group.
  • %a: permissions in octal format (e.g. 755).
  • %A: permissions in legible format (e.g. -rwxr-xr-x).
  • %X: last-access time in seconds from the time.
  • %Y: last-change time in seconds from the time.
  • %Z: last-change time (noodle) in seconds from the time.
  • %x, %y, %z: same times but in legible format legible by humans.
  • %b: number of blocks assigned.
  • %B: byte size of each block (usually 512).
  • %F: file type (regular file, directory, symbolic link, etc.).

Practical examples

To illustrate the use ofstat, let's see several common scenarios:

  • Basic information of a file:
    stat / etc / passd

    This will show the complete output with all the default fields.

  • Get only the size and permissions:
    stat -c "% n% s bytes% a" / etc / passd

    Outcome:/etc/passwd 1234 bytes 644.

  • Show the latest modification date in readable format:
    stat -c "% n was modified the% and" / var / log / syslog
  • Follow a symbolic link:
    stat -L / usr / bin / python3

    This will show the data from the real binary to which the link points, not from the link itself.

  • Get information from the file system where a directory resides:
    stat -f / home

    We'll see the type of file system, the total number of blocks, free blocks, etc.

Interpretation of the most important fields

Understanding what each field means helps diagnose permissions, performance or integrity problems.

  • Node:internal number that uniquely identifies the file within the file system. Two hard links to the same file share the same iodine.
  • hard links:account of how many file names point to the same iodine. A value of 1 indicates that there is only that name.
  • Size:number of bytes that occupy the contents of the file. For directories, it reflects the space used to store the entry list.
  • Blocks:number of disk blocks assigned. It may be larger than the size divided by the block due to internal assignments and fragmentation.
  • Timstamps:
    • Access (% X /% x):Last time the file was read.
    • Amendment (% Y /% y):last time the file content was written.
    • Change of status (% Z /% z):last time the noodle was modified (permissions, owner, links, etc.). This timestamp changes even if the content does not vary.

Stat v. ls -l

Althoughls -lis faster for a summary view,statoffers clear advantages when you need precision or you will process the output in a script:

  • Consistency:the output ofstatis not dependent on regional or terminal width; the numerical fields are always the same.
  • Format flexibility:with-cWe can extract exactly the data we need, avoiding having to make column parting.
  • File system information: lscannot show data from the file system where the file resides; for that it is necessarystat -fordf.
  • Monitoring of links:the option-Lofstatis more explicit than-Loflsand works equally well with the format option.

Conclusion

The commandstatis an essential tool for anyone who works with Linux and needs to obtain detailed and reliable information about files and file systems. Its ability to show all the attributes of the iodine, combined with the power of the format customization, makes it a higher alternative thanlswhere precision or automation is required. Dominating your options and understanding the meaning of each field will allow you to diagnose permission problems, change audit and storage optimization more effectively.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish