stat — Check file metadata
💬 "stat command"
ls -l is a summary, while stat shows all of a file's metadata precisely. Use it when investigating permission, timestamp, and inode issues.
In particular, distinguishing access/modification/change times (Atime/Mtime/Ctime) is important for troubleshooting.
- Concept: Prints the file's size, inode number, link count, owner, permissions, and timestamp information.
- Access: Last time the file was read. Because of the relatime option, it is not always updated.
- Modify: Last time the contents changed. Most useful for judging log or deployment time.
- Change: Time when metadata (permissions/owner) changed.
- Size / Blocks: Logical size and actually allocated blocks. Used to check sparse files.
Commonly used options
| Option | Description |
|---|---|
| -c <format> | Print only the fields you want in the specified format |
| -f | Print filesystem information instead of file information |
| --printf | Output with direct control over newlines |
3 essential command combinations
stat -c '%n %s %U:%G %a' /var/www/html/index.php— Extract name, size, owner, and permissions on one linestat -c '%n %y' /var/log/nginx/*.log— Compare last modified time by logstat -f -c '%T %a %b' /— Check filesystem type and block statistics
💬 "Show me an example of building evidence for an issue ticket"
Here are common situations on production servers, organized in order.
Step 1 · Check file-level information
Check the target file's permissions, owner, and timestamps.
stat /var/www/html/index.php
Step 2 · Extract only the needed fields
Use formatted output when comparing multiple files.
stat -c '%n %U:%G %a %y' /var/www/html/*.php
Step 3 · Compare before and after deployment
Verify that the deployment time and file modification time match.
stat -c '%n %y' /opt/app/current/public/index.php
How this is used in practice
1. Tracking down permission issues
Situation — The web server cannot read a specific file, causing a 403 error.
stat -c '%n %U:%G %a' /var/www/html/uploads
Result — If the owner is root:root and permissions are 700, you can immediately see that www-data cannot access it.
Point — Half of 403/500 issues are permissions. Use stat to check the owner and permissions first.
2. Verifying deployment time
Situation — After a rollback, you need to confirm that the previous file is actually in place.
stat -c '%n %y' /opt/app/current/public/index.php
Result — Check that the Modify time matches the rolled-back release time.
Point — If cache remains, checking time alone is not enough. Also check the link target.
Things to watch out for
- Access time is not always updated because of the relatime setting, so it is less reliable.
- Symbolic links show the target file's information by default. To view the link itself, check the default behavior instead of -L.
- Ctime is change time, not creation time. Some Linux filesystems do not provide creation time by default.
Actual output
This is the result of checking file owner, permissions, and timestamps, as well as log file sizes and timestamps.
!stat command output: file metadata and timestamp information
A production server
Source (HTML): https://games.aidebatehub.com/programs/linux/filesystem/stat.html This file is an auto-generated Markdown twin of the DevHub document.
AI Knowledge Hub
Visitor Comments