The ls
command is often the go-to tool for checking a file’s attributes, such as permissions, ownership, and timestamps. But what if you need more detailed information about a file or even the file system it resides on? That’s where the stat
command comes in.
What is the stat
Command?
The stat
command, short for “status,” is a powerful utility in Linux that provides detailed insights into files and file systems. While ls
gives a quick overview, stat
dives deeper, offering comprehensive data like block size, inode number, and security context. It's especially handy for examining file timestamps, such as the last access, modification, or change times.
In this guide, we’ll explore how to use the stat
command, covering its basic usage, options, and some advanced options.
Basic Syntax of stat
The syntax for the stat
command is simple and familiar:
$ stat [OPTION]... [FILE]...
You can specify one or more file names as input, with options to control the output format or behavior. Let’s begin with a basic example.
Displaying File Information
To retrieve detailed information about a file, run the stat
command followed by the file name:
$ stat log.txt
The output provides a wealth of information, including:
File: Name of the file.
Size: File size in bytes.
Blocks: Number of allocated blocks.
IO Block: Block size in bytes.
File Type: (e.g., regular file, directory, symbolic link).
Device: Device number in hexadecimal and decimal.
Inode: Unique identifier for the file in the filesystem.
Links: Number of hard links.
Ownership: User ID (UID) and group ID (GID) of the owner.
Permissions: File access permissions in symbolic and numeric modes.
Timestamps:
Access: Last time the file was accessed.
Modify: Last time the file’s content was modified.
Change: Last time the file’s metadata was modified.
Birth: Creation time (may not be supported on all systems).
This level of detail makes stat
an excellent tool for understanding a file’s properties at a glance.
Displaying File System Information
To get information about the file system instead of the file itself, use the -f
or --file-system
option:
$ stat -f log.txt
This command provides details such as:
File: Name of the file.
File System ID: Identifier in hexadecimal.
File System Type: ext2/ext3
Maximum Name Length (Namelen): The maximum allowed length of file names.
Block Information:
Fundamental Block Size: Size of each block.
Total Blocks: Total number of blocks in the file system.
Free Blocks: Number of blocks available.
Available Blocks: Free blocks available to non-root users.
Inodes:
Total: Total number of inodes.
Free: Number of free inodes.
This option is especially useful for analyzing storage capacity and file system limits.
Working with Symbolic Links
By default, stat
displays information about the symbolic link itself, not the file it points to:
$ stat symlog.txt
To dereference the symbolic link and display details about the target file, use the -L
or --dereference
option:
$ tat -L symlog.txt
This ensures you’re examining the underlying file rather than the link.
Customizing the Output
The stat
command allows for extensive customization of its output using the --format
or --printf
options:
-format
: Formats output for each file, automatically adding a newline after each entry.-printf
: Offers more control, interpreting special characters like\n
for newlines and\t
for tabs.
For example, to display only the file type:
$ stat --format="%F" log.txt
To combine multiple fields with a custom separator:
$ stat --format="%n - %F" log.txt
This will print the file name and file type separated by the - character.
Using --printf
, you can include special characters:
$ stat --printf='Name: %n\nPermissions: %a\n' log.txt
This flexibility makes stat
ideal for creating outputs tailored to your needs.
Using Terse Output for Parsing
For a compact output format suitable for scripts, use the -t
or --terse
option:
$ stat -t log.txt
This option condenses the output into a single line, making it easier to parse with other utilities (I don’t think anyone would want to see terse output (≧▽≦)).
Learning More
To explore additional options and format directives, consult the stat
man pages or use the --help
command:
$ man stat
$ stat --help
These resources provide a comprehensive list of capabilities, ensuring you can leverage stat
to its fullest.
Wrapping Up
The stat
command is a helpful tool for checking detailed information about files and file systems. It offers options to view timestamps, customize output, and follow symbolic links. With the basics covered, you can now use stat
confidently in your tasks.