Linux stat Command with Examples

The stat command in Linux is a powerful tool for retrieving detailed information about files and file systems. It displays comprehensive data including file permissions, timestamps, ownership, inode numbers, and filesystem properties. This command proves invaluable for system administration, troubleshooting, and security analysis.

Basic File Information

The most common usage displays complete file metadata

$ stat /var/log/syslog
File: /var/log/syslog
Size: 602244        Blocks: 1200       IO Block: 4096   regular file
Device: 803h/2051d  Inode: 175419      Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 0/ root)   Gid: ( 0/ root)
Access: 2022-11-15 11:45:41.000000000 -0500
Modify: 2022-11-15 11:45:41.000000000 -0500
Change: 2022-11-15 11:45:41.000000000 -0500
 Birth: -

Output Fields Explained

Field Description
File Full path of the file
Size File size in bytes
Blocks Number of filesystem blocks allocated
IO Block Filesystem block size in bytes
Device Device ID containing the file
Inode Unique inode number
Links Number of hard links
Access File permissions and ownership
Modify Last data modification time
Change Last metadata change time
Birth File creation time (if supported)

Filesystem Information

Use the -f option to display filesystem statistics instead of file information

$ stat -f /var/log/syslog
File: "/var/log/syslog"
Filesystem: ext4
Block size: 4096
Blocks: Total: 12658517
Free: 10106288
Available: 9996658

This shows filesystem-level details useful for disk space monitoring and storage analysis.

Custom Format Output

The -c and --printf options allow custom output formatting using format specifiers

$ stat --printf='%U<br>%G<br>%i<br>%Z<br>' /var/log/secure

Common Format Specifiers

Specifier File Information Filesystem Information
%n Filename Filesystem name
%s File size in bytes Block size
%U Owner username N/A
%G Group name N/A
%i Inode number N/A
%Z Last change time (epoch) N/A
%a Access permissions (octal) Free blocks (non-root)
%b Blocks allocated Total blocks

Example showing filesystem information with custom format

$ stat --printf='Name: %n\nFree blocks: %a\nTotal blocks: %b<br>' /

Terse Output Format

The -t option provides machine-readable output in a single line

$ stat -t /var/log/syslog
/var/log/syslog 127952 272 81a4 0 0 802 2185068 1 644 1671482093 1671479542 1671479542 0 4096

This format is ideal for shell scripts and automated processing.

Common Use Cases

  • Security auditing Check file permissions and ownership

  • Backup verification Compare timestamps and file sizes

  • Disk space monitoring Analyze filesystem usage with -f

  • Troubleshooting Examine file access patterns and metadata

  • Script automation Extract specific file attributes with custom formats

Accessing Documentation

For complete format specifier reference and advanced options

$ man stat

Note that some shells may have built-in stat implementations with different options.

Conclusion

The Linux stat command provides comprehensive file and filesystem information essential for system administration and troubleshooting. Its flexible formatting options make it suitable for both interactive use and automated scripts, while the detailed metadata helps users understand file properties and system behavior.

Updated on: 2026-03-17T09:01:39+05:30

882 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements