stat Command in Linux



Linux is a robust operating system that offers a lot of commands to simplify file handling, system management, and debugging. One such command is stat, which can be used to fetch detailed information about directories and files.

Unlike simple file listing commands like ls, the stat command offers complete metadata, including file permissions, ownership, timestamps, and more. This command is extremely useful for system administrators, programmers, and anyone who handles Linux files.

Table of Contents

Here is a comprehensive guide to the options available with the stat command −

Syntax of stat Command

The syntax of the stat command in Linux is quite straightforward. The basic format is −

stat [OPTION]... FILE...

Here −

  • OPTION refers to various flags that modify the output.
  • FILE refers to the file or directory whose detailed metadata you want to retrieve.

Understanding File Timestamps in stat Command

The stat command provides three important timestamps associated with files −

  • Access Time (atime): The last time the file was accessed.
  • Modification Time (mtime): The last time the file's content was modified.
  • Change Time (ctime): The last time file metadata (such as permissions or ownership) was changed.

stat Command Options

The stat command comes with various options that allow users to customize the output as per their needs. Below are some commonly used options −

Option Description
-L, --dereference Follow symbolic links and display information about the actual file instead of the link itself.
-f, --file-system Show information about the file system where the file is stored (like total/free space).
--cached=MODE Control how cached file attributes are used, helpful for remote file systems.
-c, --format=FORMAT Customize the output format by selecting specific file attributes.
--printf=FORMAT Like --format, but allows special characters (like \n for new lines) and removes automatic newlines.
-t, --terse Display the output in a compact, single-line format.
--help Show all available options and their descriptions.
--version Display the version number of the stat command installed.

Examples of stat Command in Linux

To better understand how the stat command works, let's go through a few practical examples.

  • Viewing Detailed File Informations
  • Displaying Specific File Attributes
  • Checking Inode Information of a File
  • Viewing Files in a Compact Format
  • Verifying a Directory

Viewing Detailed File Information

Suppose you have a file named report.txt. To check its metadata, run −

stat report.txt

This command will display various attributes, such as −

  • File Size − Indicates the total size of the file in bytes.
  • Last Accessed Time − Shows the last time the file was accessed.
  • Last Modified Time − Displays the last time changes were made to the file.
  • Permissions − Lists the file’s access permissions (read/write/execute).
  • Owner and Group − Specifies who owns the file and the associated group.
stat Command in Linux1

This is useful for auditing file changes, security settings, and system monitoring.

Displaying Specific File Attributes

If you want to extract only certain attributes such as file size, permissions, and owner details, use −

stat -c "%s %A %U %G" report.txt

Here’s what this command extracts −

  • %s − File size in bytes.
  • %A − File permissions.
  • %U − File owner.
  • %G − Group owner.

This command will output the file size, permissions, owner, and group in a custom format.

stat Command in Linux2

The above output tells us that the file is 25 bytes, has rw-r--r-- permissions, belongs to user 'linux', and is part of the group 'linux'.

Checking Inode Information of a File

If you want to analyze a file inode information, run −

stat -c "%i %h %F" report.txt

This retrieves −

  • %i − Inode number (a unique identifier for the file within the filesystem).
  • %h − Hard link count (how many file entries point to this inode).
  • %F − File type (regular file, directory, symbolic link, etc.).
stat Command in Linux3

The above output tells us that report.txt has inode 21074, has 1 hard link, and is classified as a regular file.

Viewing Files in a Compact Format

For users who prefer concise output, the -t option simplifies the display −

stat -t report.txt

Instead of multiple rows, this outputs information in a single-line format, which is useful for scripting and quick inspection.

stat Command in Linux4

Verifying a Directory

The stat command is not limited to files; it works for directories as well. For example −

stat my_directory/

This returns metadata such as −

  • Number of Links − Indicates how many references (subdirectories or files) exist within it.
  • Permissions − Defines read/write/execute access rights.
  • Timestamps − Shows when the directory was last accessed or modified.
stat Command in Linux5

The above output tells us that the Documents directory is 4096 bytes, has 2 references, and was last modified on April 21, 2025.

Real-Life Use Cases for stat

  • Tracking File Changes − Developers and system administrators use stat to monitor last modified times.
  • Checking File Size before Processing − Useful in automation scripts to prevent processing large files.
  • Identifying Symbolic Links − Helps in verifying if a link is pointing to the correct file.
  • Debugging File Permissions − Useful when troubleshooting access-related errors.

Differences Between stat and ls

While both stat and ls -l show information for files, stat shows more specific metadata like timestamps, inode number, and block size. ls -l command is mainly applied with listing permissions and files.

Conclusion

The stat command is an extremely helpful command for obtaining detailed file and directory information on Linux. Its ability to display detailed metadata simplifies users' ability to monitor file changes, verify attributes, and debug file permission issues effectively. Whether you are a beginner or a seasoned user, mastering the stat command will definitely make you more efficient at handling files.

Using multiple options and real-world examples, you can customize the stat command according to your own needs. Once you master this command, it will be helpful to you in troubleshooting, scripting, and system administration.

Advertisements