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
| Option | Description |
-L | Follow symbolic links and display information for the target file instead of the link itself. |
-f | Display file system status (type, capacity, etc.) instead of individual file information. |
-c <format> | Customize the output using a specific format string (equivalent to --format). |
-t | Output information in a concise (terse) format, ideal for processing in shell scripts. |
Main Format Specifiers (for -c option)
| Symbol | Description |
%n | File name |
%a | Access rights in octal format (e.g., 644) |
%A | Access rights in human-readable format (e.g., -rw-r–r–) |
%u | User ID (UID) of the owner |
%U | User name of the owner |
%g | Group ID (GID) of the owner group |
%G | Group name of the owner group |
%x | Last access time (atime) |
%y | Last modification time (mtime / content change) |
%z | Last 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 filenameto 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
-toption outputs information in a single space-separated line, making it easier for tools likeawkto process.
Important Notes
- GNU vs. BSD Differences: This guide covers the GNU version of
statstandard in Linux. The BSD version (found in macOS or FreeBSD) uses different options (e.g.,-ffor 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 -loften shows time only up to the minute,statprovides 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.
