×


Linux Stat Command Examples

The good old ls command is a favorite tool for listing files or directories contained in a directory. It can also list attributes such as file permissions, ownerships, file & directory size and modification date. But that is as far as it can go.

If you want more detailed information about a file, use the stat command. The stat command provides detailed information about a particular file or file system.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform Linux Systems queries.

In this context, we shall look into how to use the stat command to get more detailed information about file systems.


Stat Basic Syntax

Th command for stat is given below:

$ STAT [OPTION] [FILE]

Options

The following are arguments that can be used along with the stat command:

  • -f : Prints the status of the filesystem.
  • -c –format=FORMAT: Displays the output in a specified format.
  • -L, –dereference: used to follow symlinks.
  • -t, –terse: used to display file information in terse form.


Stat command without arguments

In its basic form, the stat command can be used to check the information of a file, as shown:

$ stat [ file]

When no argument is passed to stat it displays the following information:

  • File – This is the name of the file being displayed.
  • Size – gives the size of the file in bytes.
  • Blocks – specifies the number of allocated blocks a file takes.
  • IO Block – specifies the size of every block in bytes..
  • File type – the type of the file.
  • Device -Displays the device number decimal and hex.
  • Inode – displays the inode number.
  • Links -specifies the number of hard links available.
  • Access – displays the file permissions in the numeric and symbolic forms.
  • UID – specifies the user ID.
  • GID – specifies the group ID and owner.
  • Context – Specifies the SELinux security context.
  • Access -this is the last time the file was accessed.
  • Modify – shows the last time the file was modified.
  • Change – The last time the file's attribute was changed.
  • Birth – Time the file was created.


How to use stat to display information about the file system ?

Additionally, you can display information about a mount point or filesystem as shown.

$ stat -f /opt

When the option -f is passed to the stat command, it displays the following information:

  • File – this is the name of the file.
  • ID – Displays the ID of the file system in hex.
  • Namelen – maximum file length
  • Fundamental block size – Defines the size of each block in the file system.


Blocks:

  • Total – Total number of blocks in the file system.
  • Free – specifies the number of free blocks in the file system.
  • Available – specifies the number of free blocks available to non-root users.


Inodes:

  • Total – specifies the total number of inodes available in the file system.
  • Free – specifies the number of free inodes available.


How to Customize Output with stat command ?

The stat command allows you to customize the output. Use the –printf or –format options to display specific information.

For example, to print only the number of hard links available in a file use the %h format sequence. It automatically prints the output on a new line in cases where you have specified several options:

$ stat --format=%h [FILE]

To print the file type information:

$ stat --format=%F [FILE]

When using –printf, you need to use \n to print in a new line when displaying multiple operands. 


How to use stat command to Enable or follow symlinks ?

When we run the stat command against a symlink it only provides information about the link. It does not provide information about the file that the link points to. Here's an example.

$ stat /etc/resolv.conf

Use the -L option to follow symlinks and get the information about the file it points to:

$ stat -L /etc/resolv.conf


How to Display information in terse form using stat command ?

Use the -t option to display file information in terse form.

$ stat -t file.txt


[Need assistance in fixing Linux System errors? We can help you. ]


Conclusion

This article covers how to use the Linux stat command. Basically, The stat command gives important information about filesystems. stat command is a useful utility for viewing file or file system status. It retrieves information such as file type; access rights in octal and human-readable; SELinux security context string; time of file birth, last access, last data modification, last status change in both human-readable and in seconds since Epoch, and much more. Also, It has an option to specify a custom format instead of the default, for displaying information.