Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
5 ‘stat’ Command Examples for Linux Newbies
The stat command is one of the most useful utilities in Linux for examining detailed file and directory information. It provides comprehensive metadata about files including permissions, timestamps, sizes, and filesystem properties. This article explores five essential stat command examples that every Linux newcomer should know.
Getting Basic File Information
The simplest use of the stat command is to display comprehensive information about a file or directory. Simply use the following syntax
stat filename
For example, to examine a file called example.txt
stat example.txt
This command produces output showing the file's size, permissions, timestamps, inode number, and other metadata in a detailed format.
Sample Output
File: example.txt Size: 1024 Blocks: 8 IO Block: 4096 regular file Device: 801h/2049d Inode: 123456 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1000/ user) Gid: ( 1000/ user) Access: 2024-01-15 10:30:00.000000000 +0000 Modify: 2024-01-15 09:45:00.000000000 +0000 Change: 2024-01-15 09:45:00.000000000 +0000 Birth: -
Finding File Type with Format Specifiers
The stat command supports format specifiers to extract specific information. To determine the file type, use the %F format specifier
stat --format='%F' filename
Example
stat --format='%F' example.txt
regular file
This returns the file type, such as "regular file", "directory", "symbolic link", or "block special file".
Displaying File Permissions
To view file permissions in human-readable format, use the %A format specifier
stat --format='%A' filename
For octal permission format, use %a
stat --format='%a' example.txt
644
The output shows permissions where 644 means owner has read/write (6), group has read (4), and others have read (4) permissions.
File Size and Timestamps
Several format specifiers help extract size and time information
| Format | Description | Example Command |
|---|---|---|
| %s | File size in bytes | stat --format='%s' file.txt |
| %x | Last access time | stat --format='%x' file.txt |
| %y | Last modification time | stat --format='%y' file.txt |
| %z | Last status change time | stat --format='%z' file.txt |
Human-Readable File Size
To display file size in a more readable format, combine stat with numfmt
stat --format='%s' example.txt | numfmt --to=iec
1.0K
Advanced File System Information
The stat command can reveal detailed filesystem metadata
Inode Number
stat --format='%i' example.txt
Number of Hard Links
stat --format='%h' example.txt
File System Block Size
stat --format='%o' example.txt
Device ID
stat --format='%d' example.txt
Practical Examples
Here's a comprehensive example combining multiple format specifiers
stat --format='File: %n%nSize: %s bytes%nPermissions: %A (%a)%nOwner: %U%nGroup: %G%nModified: %y' example.txt
File: example.txt Size: 1024 bytes Permissions: -rw-r--r-- (644) Owner: user Group: user Modified: 2024-01-15 09:45:00.000000000 +0000
Conclusion
The stat command is an essential tool for Linux file system management, providing detailed metadata about files and directories. By mastering these format specifiers and examples, Linux users can efficiently gather file information for system administration, troubleshooting, and automation tasks.
