stat
Overview
The stat
command displays detailed information about files and filesystems, including permissions, timestamps, size, and inode details.
Syntax
stat [options] file...
Common Options
Option | Description |
---|---|
-c format |
Custom format |
-f |
Display filesystem status |
-L |
Follow symbolic links |
-t |
Terse format |
--printf=format |
Printf-style format |
File Information Fields
Field | Description |
---|---|
File | Filename |
Size | File size in bytes |
Blocks | Number of blocks allocated |
IO Block | Filesystem block size |
Device | Device ID |
Inode | Inode number |
Links | Number of hard links |
Access | File permissions |
Uid/Gid | User and group IDs |
Access time | Last access time |
Modify time | Last modification time |
Change time | Last status change time |
Birth time | File creation time (if supported) |
Key Use Cases
- View detailed file information
- Check file permissions and ownership
- Analyze timestamps
- Filesystem analysis
- Debugging file issues
Examples with Explanations
Example 1: Basic File Information
stat file.txt
Shows complete file information
Example 2: Filesystem Information
stat -f /home
Displays filesystem statistics
Example 3: Custom Format
stat -c "%n %s %y" file.txt
Shows filename, size, and modification time
Format Specifiers
Specifier | Description |
---|---|
%n |
Filename |
%s |
Total size in bytes |
%b |
Number of blocks |
%f |
Raw mode in hex |
%F |
File type |
%a |
Access rights in octal |
%A |
Access rights in human readable form |
%u |
User ID |
%g |
Group ID |
%U |
User name |
%G |
Group name |
%x |
Time of last access |
%y |
Time of last modification |
%z |
Time of last change |
Common Usage Patterns
Check permissions:
stat -c "%a %n" file.txt
Compare timestamps:
stat -c "%y %n" file1 file2
Find inode number:
stat -c "%i" file.txt
Timestamp Analysis
Understanding timestamps: - Access time (atime): Last read - Modify time (mtime): Last content change - Change time (ctime): Last metadata change - Birth time (btime): Creation time (ext4, btrfs)
Performance Analysis
- Fast operation
- No file content reading
- Minimal system resources
- Good for scripting
- Efficient metadata access
Additional Resources
Best Practices
- Use custom formats for scripting
- Check filesystem support for features
- Understand timestamp meanings
- Use with other tools for analysis
- Consider timezone effects
Scripting Examples
Find files modified today:
stat -c "%y %n" * | grep $(date +%Y-%m-%d)
Check if file is executable:
[[ $(stat -c "%a" file) -ge 100 ]] && echo "Executable"
Compare file ages:
stat -c "%Y" file1 file2 | sort -n
Filesystem Information
Using -f
option shows: - Filesystem type - Block size - Total blocks - Free blocks - Available blocks - Total inodes - Free inodes
Troubleshooting
- Permission denied errors
- Symbolic link handling
- Filesystem compatibility
- Timestamp interpretation
- Format string errors
Integration Examples
With find:
find . -name "*.txt" -exec stat -c "%n %s" {} \;
With awk:
stat -c "%s %n" * | awk '$1 > 1000000'
Monitoring script:
stat -c "%y" important.txt > timestamp.log