Skip to content

stat Command: Examining File Status

Summary

The stat command is a powerful utility in Linux that displays detailed information about files and file systems, including file size, access times, permissions, and inode number.

Introduction

The stat command provides a comprehensive overview of a file or file system's attributes. It goes beyond what ls offers, providing access to low-level details that can be crucial for debugging, understanding file system behavior, and ensuring data integrity. It is particularly useful for scripting and automation where detailed file metadata is required.

Use Case and Examples

Display basic file information

stat myfile.txt
This command will display basic information about myfile.txt, including its size, modification times, and access rights.

Display file access time

stat -c %x myfile.txt
This command extracts and displays only the last access time of the file. The -c option allows you to specify a format string to control the output.

Display file size in bytes

stat -c %s myfile.txt
This displays the file size in bytes.

Display the inode number

stat -c %i myfile.txt
This shows the inode number of the file. The inode number is a unique identifier for the file within the file system.

Display file permissions in octal

stat -c %a myfile.txt
This command displays file permissions in octal format, which is useful for scripting and system administration tasks.

Checking symbolic link information

stat -L mylink
If mylink is a symbolic link, this command will show the status of the file that the link points to, not the link itself. Without -L, it shows the status of the symbolic link.

Commonly used flags

Flag Description Example
-c FORMAT Uses a specified FORMAT instead of the default; outputs a newline after each use of format. stat -c %n myfile.txt (Displays the file name only)
-f Display file system status instead of file status. stat -f / (Displays file system information for the root directory.)
-L Follow symbolic links. Display information about the file the link points to, not the link itself. stat -L mylink
-t Print the information in a concise, human-readable format. stat -t myfile.txt
-x Display the entire stat structure in hexadecimal format. This is mostly useful for debugging. stat -x myfile.txt
--file-system Display file system status instead of file status. Equivalent to -f. stat --file-system /
--dereference Follow symbolic links. Equivalent to -L. stat --dereference mylink


Share on Share on

Comments