Stat command: Usage and examples

The stat Linux command is one of the best ways to view the full details of any file that is stored on a Linux system. It is installed by default on all distributions, so there is nothing extra to install, and is basically a one stop shop for viewing file permissions, timestamp info like access times and modification times, and various other metadata for any file on your system. It also comes with a few handy options, allowing us to tailor the output for specific needs, and view information about all file types, including symbolic links, pseudo files, and others.

In this tutorial, you will learn how to use the stat command in Linux through command line examples. It is one of the more basic and essential Linux commands that users of all levels are recommended to learn, and you will surely find yourself using it frequently. Follow along below to learn about the various options that you can use with this command.

In this tutorial you will learn:

  • How to use the stat command on Linux
Stat command: Usage and examples
Stat command: Usage and examples
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software stat
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Stat command: Usage and examples




The stat command displays information for any file or directory on Linux. Its most basic syntax involves specifying the path to a file, with no further options:

$ stat example_file.txt
Output of the stat command showing file information on Linux
Output of the stat command showing file information on Linux

The stat command’s output gives us the following information:

  • File name
  • Size of the file
  • How many blocks the file consumes
  • IO block number
  • The type of file it is
  • The device number on which the file resides
  • What number inode the file is
  • Number of hard links to the file
  • The file permissions in absolute and symbolic mode
  • UID of the file owner for the file
  • GID of the group owner for the file
  • Access time (timestamp of when the file was last accessed)
  • Modify time (timestamp of when the file was last modified)
  • Change time (timestamp of when the file’s metadata was last changed)
  • Birth time (timestamp of when the file was first created)

Display File System Information




You can use the -f option to display information about the file system instead of the file:

$ stat -f example_file.txt
Viewing the file system information with the stat command
Viewing the file system information with the stat command

The stat command once again presents us with a plethora of information. Here is what each thing means:

  • The file name
  • The ID of the file system in hexadecimal format
  • Namelen (max allowed length for file names)
  • The file system’s block size
  • The total, free, and available blocks on the file system
  • The total and free number of inodes on the file system

Follow Symlinks

As mentioned earlier, the stat command can also display information about symbolic links. If, instead, you would like information about the file that a symbolic links to, you can use the deference option, which is enabled with the -L flag.

$ stat -L symbolic_link

Formatting Options

Sometimes the detailed output from the stat command can be more information than we are after. In this case, it is possible to format the output to isolate just the relevant information you want to see. The syntax as is follows:

$ stat --format=FORMAT example_file.txt

For a full list of format options you can specify, simply check the stat manual page.




The stat manual page lists all of the formatting options that can be used
The stat manual page lists all of the formatting options that can be used

Closing Thoughts

In this tutorial, we saw how to use the stat on a Linux system. This command is extremely useful for users of all levels, and gives us all the information you could need about any type of file or directory on the Linux file system. We also learned about the stat command’s most useful options, and how to interpret its output.