Linux stat Command with Examples


The 'stat' command in Linux is a powerful tool for retrieving information about files and file systems. It offers a wealth of details about the characteristics of a specific file or directory, including file permissions, timestamps, ownership, and inode number. This command has various applications, from file troubleshooting to security analysis. In this article, we will delve into the Linux 'stat' command, exploring its options and features, and providing practical examples of how it can be utilized in real-world scenarios. With a deep understanding of the 'stat' command, Linux users can gain a better understanding of the properties and state of the files and directories on their system.

Here are some examples showing the use of the stat command.

  • The "stat" command is an essential tool for displaying information about a specific file or directory in Linux. The command "stat /var/log/syslog" is used to get an in-depth look at the properties and attributes of the file "/var/log/syslog", which is usually a log file that contains information about various system events and processes. The "stat" command provides a wealth of information, including the file's inode number, file type, permissions, timestamps, size, and ownership, which can be useful in various scenarios like troubleshooting, analysis, and security. By using the "stat" command on the "/var/log/syslog" file, a Linux user can gain valuable insight into the state and behavior of their system.

Input

$ stat /var/log/syslog

Output

The output of the "stat /var/log/syslog" command will vary depending on the specific system and file, but it will typically display information similar to the following 

File: /var/log/syslog
Size: 602244        Blocks: 1200       IO Block: 4096   regular file
Device: 803h/2051d  Inode: 175419      Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 0/ root)   Gid: ( 0/ root)
Access: 2022-11-15 11:45:41.000000000 -0500
Modify: 2022-11-15 11:45:41.000000000 -0500
Change: 2022-11-15 11:45:41.000000000 -0500
 Birth: -

This information includes −

  • File − the name and path of the file being queried

  • Size − the size of the file in bytes

  • Blocks − the number of blocks used to store the file

  • IO Block − the size of each block in bytes

  • Device − the device number of the file system containing the file

  • Inode − the inode number of the file

  • Links − the number of links to the file

  • Access − the access permissions of the file and the user and group ownership of the file

  • Modify − the time the file was last modified

  • Change− the time the file's metadata (permissions, ownership, etc.) was last changed

  • Birth − the time the file was created (if available)

  • The Linux command "stat -f /var/log/syslog" displays information about the file system in which the file "/var/log/syslog" is located. The "-f" option specifically focuses on the file system and provides details such as block size, total blocks, free blocks, and available blocks. This information is useful for analyzing disk space, troubleshooting storage issues, and monitoring the health of the file system. Running the "stat -f" command on the "/var/log/syslog" file provides valuable information about the state and properties of the file system it is located on in a Linux system.

Input

$ stat -f /var/log/syslog

Output

The output of the "stat -f /var/log/syslog" command would display information about the file system in which the file "/var/log/syslog" resides. The output might look similar to this −

File: "/var/log/syslog"
Filesystem: ext4
Block size: 4096
Blocks: Total: 12658517
Free: 10106288
Available: 9996658
  • The use of the "stat -f /" command in Linux offers a glimpse into the health and properties of the root file system. By utilizing the "-f" option, which stands for "file system," information is displayed including the block size, total blocks, free blocks, and available blocks. This information can be crucial in analyzing disk usage, addressing disk space issues, and keeping an eye on the overall health of the file system. With the execution of this command, Linux users can gain valuable insights into the state of their root file system.

Input

$ stat -f /

Output

File: "/"
Filesystem: ext4
Block size: 4096
Blocks: Total: 12658517  
Free: 10106288  
Available: 9996658
Inodes: Total: 21544960   
Free: 21005263

Custom Format to Display Information

  • The 'stat' command in Linux provides the option of customizing the format of its output. This allows the user to display only the specific information they need, rather than the default format which may contain unnecessary details. To specify a custom format, one can use the '-c' flag followed by the desired format string. The '--printf' option is another way of customizing the output format, enabling the use of backslash escape sequences and offering more control over line breaks. By using '--printf' and including '
    ' in the format string, a user can precisely display the desired information in the desired format.

$ stat --printf='%U
%G
%C
%z
' /var/log/secure

The significance of the format sequences for files in the previous example −

  • %U – The file's owner's username

  • %G – The file's group name

  • %C – The file's inode number

  • %z – The file's modification time in seconds since the Epoch

  • Here's an illustration of how the accepted format sequences for file systems can be applied in practice.

$ stat --printf='%n
%a
%b
' /

The significance of the format sequences for files in the previous example −

  • %n – shows the name of file

  • %a – displays available free blocks for non-administrative users

  • %b – displays the total number of data blocks in the file system.

  • The option "-t" can be utilized to present the information in a succinct manner.

$ stat -t /var/log/syslog

The output of the given example is as follows 

  File: '/var/log/syslog'
  Size: 127952       Blocks: 272        IO Block: 4096   regular file
Device: 802h/2050d  Inode: 2185068     Links: 1
Access: (0644/-rw-r--r--)  Uid: (  0/    root)   Gid: (  0/    root)
Access: 2022-12-19 20:13:13.645865908 +0000
Modify: 2022-12-19 19:32:22.797664688 +0000
Change: 2022-12-19 19:32:22.797664688 +0000
 Birth: -
  • It's worth mentioning that your shell may possess its own implementation of the stat command, and for more information on the supported options, you should consult your shell's documentation. If you're looking for a comprehensive list of all the accepted output format sequences, simply refer to the manual page for the stat command.

$ man stat 

The "man stat" command in Linux is an excellent resource for understanding the stat command. By accessing the manual page, users can gain insight into the various aspects of a file, such as its size, ownership, accessibility, and time-related information. The manual page provides an in-depth look at the options available when using the stat command, along with clear examples and explanations to aid in its usage.

Conclusion

To put it succinctly, the Linux stat command serves as a powerful means of gathering information about a file's attributes. With a range of options at its disposal, it empowers users to retrieve information such as a file's dimensions, who it belongs to, access rights, and time-related details. The manual page of the stat command is an abundant source of information on how to utilize it, providing examples and clear explanations of the information it provides. It is also important to keep in mind that some shells may have their own variations of the stat command, so it is advisable to refer to their respective documentation for further details. In conclusion, the Linux stat command is an invaluable resource for those looking to comprehend the characteristics of a file in their system.

Updated on: 28-Jul-2023

207 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements