[Linux] View Detailed File and File System Information with the stat Command

目次

Overview

The stat command provides a comprehensive look at file and directory metadata that the standard ls command cannot fully display. It covers details such as inode numbers, exact file sizes, block counts, and the three types of timestamps (access, modify, and change). Additionally, it can be used to check information about the file system itself, such as the type and available capacity where the file is stored.

Specifications (Arguments and Options)

Syntax

stat [options] filename

Main Arguments and Options

OptionDescription
-LFollow symbolic links and display information for the target file instead of the link itself.
-fDisplay file system status (type, capacity, etc.) instead of individual file information.
-c <format>Customize the output using a specific format string (equivalent to --format).
-tOutput information in a concise (terse) format, ideal for processing in shell scripts.

Main Format Specifiers (for -c option)

SymbolDescription
%nFile name
%aAccess rights in octal format (e.g., 644)
%AAccess rights in human-readable format (e.g., -rw-r–r–)
%uUser ID (UID) of the owner
%UUser name of the owner
%gGroup ID (GID) of the owner group
%GGroup name of the owner group
%xLast access time (atime)
%yLast modification time (mtime / content change)
%zLast status change time (ctime / attribute change)

Basic Usage

When run without any options, the command displays all available metadata for the specified file.

Command

# Display information for a standard file
stat production.log

Execution Result

  File: production.log
  Size: 2048      	Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d	Inode: 131075      Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/    user)   Gid: ( 1000/    user)
Access: 2026-01-17 10:00:00.000000000 +0900
Modify: 2026-01-16 23:55:00.000000000 +0900
Change: 2026-01-16 23:55:00.000000000 +0900
 Birth: 2026-01-01 12:00:00.000000000 +0900

Note: The “Birth” (creation time) may not be displayed depending on your file system or OS version.

Practical Commands

Check File System Information

Instead of the file itself, this shows details about the disk partition (file system) where the file resides, including block size and total inodes.

# Display file system status (type, block size, inodes, etc.)
stat -f /var/www/html/index.html

Example Output:

  File: "/var/www/html/index.html"
    ID: 80100000000 Namelen: 255     Type: ext2/ext3
Block size: 4096       Fundamental block size: 4096
Blocks: Total: 25600000   Free: 18000000   Available: 17500000
Inodes: Total: 6400000    Free: 6200000

Display Formatted Information (ls-style)

Using the -c option, you can extract only the items you need. For example, you can display permissions, user, group, change time, and filename in a single line.

# Display specific attributes: permissions, user, group, ctime, and filename
stat -c "%A %U %G %z %n" sample_script.sh

Example Output:

-rwxr-xr-x app_user app_group 2026-01-17 10:15:30.555555555 +0900 sample_script.sh

Customization Points

  • Script Integration: If you only need the modification time for a script variable, use stat -c %y filename to avoid complex text parsing.
  • Unix Time: To get timestamps as seconds since 1970 (Unix epoch), use %X (atime), %Y (mtime), or %Z (ctime). This is very helpful for mathematical calculations in scripts.
  • Terse Mode: The -t option outputs information in a single space-separated line, making it easier for tools like awk to process.

Important Notes

  • GNU vs. BSD Differences: This guide covers the GNU version of stat standard in Linux. The BSD version (found in macOS or FreeBSD) uses different options (e.g., -f for formatting) and different format symbols.
  • Timestamp Interpretation: Linux did not have a standard way to store “Creation Time” for a long time. In many environments, the “Birth” field might be blank (-) or missing.
  • Precision: While ls -l often shows time only up to the minute, stat provides precision down to the nanosecond.

Applications

List Permissions and Owners for All Files in a Directory

Combined with the find command, you can generate a CSV-like report of permissions and owners for an entire directory tree.

# Output "octal permissions, owner, filename" for all files in the current directory
find . -type f -exec stat -c "%a,%U,%n" {} \;

Example Output:

644,root,./etc/config.json
755,www-data,./var/www/html/index.php
600,mysql,./var/lib/mysql/ibdata1

Summary

The stat command is essential when the “resolution” of ls is not enough. It is a vital tool for system troubleshooting, such as checking for inode exhaustion or comparing timestamps at the nanosecond level to determine the sequence of file changes. By mastering the formatting features, you can flexibly generate audit logs and monitoring scripts to improve your system administration efficiency.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次