The Linux Stat command: see detailed file information

Introduction

In the Linux environment, knowing the details of a file goes far beyond its name or size. The commandstatallows you to obtain a complete view of the metadata associated with any file or directory, from permissions and timstamps to the noodle number and the file system block in which you reside. This tool is essential for system managers, developers and any user who needs to perform audits, debug permissions or simply understand how the system manages storage.

Basic syntax

The simplest way to usestatis:

stat [options] file

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
  • Date of last access, modification and change
  • Number of noodles and hard links

This default output is useful for rapid inspection, but the real power ofstatis revealed when using your format options.

Most useful options

The true power ofstatis in your format options. With the option-cor--formatYou can define exactly which fields you want to see and in which order.

  • -c FORMAT: specifies a format chain using sequences like%n(name),%s(size),%U(owner),%G(group),%a(octal permits),%A(symbolic permits),%X(last-access time in seconds from epoch),%Y(last amended time),%Z(last-change time).
  • -f: shows information from the file system where the file resides, instead of the file itself.
  • -L: follows symbolic links and shows the information in the target file.
  • -t: shows output in a tender, useful form for scripts that require a compact format.

In addition, you can combine several options for more specific information. For example,stat -Lc '%n %s %U'show the name, size and owner of the file to which a symbolic link points.

Practical examples

1. See only the size and name

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

This will print something like:

archivo.txt 1245

2. Show permissions in readable format

stat -c '% n% to% A' archivo.txt

Output:

file.

3. Get the latest modification date in readable format

stat -c '% n% and' archivo.txt

The Specifier%yshows the modification date in readable format.

4. File system information

stat -f -c '% T% S% b% f' / route / to / directory

Here.%Tis the type of file system,%Sthe block size in bytes,%bthe total number of blocks and%fthe free blocks.

5. Follow a symbolic link

stat -L -c '% n% s' link _ symbolic

This will show the size of the file to which the link points, not the link itself.

6. Compare two file timstamps

if [ $(stat -c '% Y' archive1) -gt $(stat -c '% Y' archivo2); then echo "archivo1 is newer"; fi

This Bash fragment uses the Specifier%Y(time of modification in seconds from epoch) to determine which of two files has been modified most recently.

Common cases of use

  • Security audit: compare permissions and critical file owners to detect inappropriate settings.
  • Maintenance scripts: verify that a file has been modified after a certain date using%Yand compare to the present time, firing alerts or corrective actions.
  • Performance diagnosis: know the number of blocks assigned and the block size to estimate I / O and plan storage training.
  • Storage management: use the option-fto review the type and capacity of the file system where a volume is saved, useful before creating new file systems or migrating data.
  • Additional support: identify changed files from a given timestamp by%Z(last change time) and copy only those files.

Tips and tricks

  • Combinestatwithgreporawkto extract only one field:stat -c '%s' archivo | awk '{print $1}'.
  • In Bash, you can use process expansion to compare timstamps:if [ $(stat -c '%Y' archivo1) -gt $(stat -c '%Y' archivo2) ]; then echo "archivo1 es más nuevo"; fi.
  • Remember that the timstamps shown by%X, %Yand%Zare in seconds from the epoch; to convert them to a legible date usesdate -d @$(stat -c '%Y' archivo).
  • If you need information from many files at once, usefindwith-exec stat ...orprint0to avoid problems with names containing spaces.
  • To get a simple CSV output that can be imported to a spreadsheet, use something like:stat -c '%n,%s,%U,%G,%y' *.txt > reporte.csv.

Conclusion

The commandstatis an essential tool for any advanced Linux administrator or user who needs to deepen file metadata. Its format flexibility allows the output to be adapted to almost any requirements, from simple size verifications to complex security audits and automation scripts. Dominating your options will save you time and give you much more thin control over the file system, facilitating maintenance, debugging and capacity planning.

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

EnglishenEnglishEnglish