This tutorial will walk you through how you can use the stat command on your Linux system.
The stat command is a useful utility that allows you to retrieve information about a file on your system.
This utility is helpful for cases where the ls command doesn’t give you quite enough information. For example, it can provide the access or modification time, the inode, file type, and more.
In addition to providing you with the status of a file, the stat command can also provide you with status about the filesystem itself.
Below we will show you the different ways that you can utilize the stat command.
Table of Contents
- Syntax of the stat Command
- Using the stat Command on a File
- Using the stat Command on Multiple Files
- Following Symbolic Links with the stat Command
- Using stat to Check the Filesystems Status
- Getting a terse Report from the stat Command
- Customizing the Output of the stat Command
Syntax of the stat Command
The syntax for the stat command is simple. At its bare minimum, all you need to do is provide the file you want to get the status of.
Below you can see the syntax for the stat command on Linux.
stat [OPTIONS]... FILE...
You can utilize the options field(
[OPTIONS]) to control the behavior of the stat command. For example, you can control the data output by utilizing the “
--format” option or follow symlinks using”
The options field is optional and is not required for the stat command to work.
However, what is required is the filename field (
FIELD) that you want to get the status of. If you want to get the stat of multiple files, you can enter as many filenames as you would like.
Using the stat Command on a File
Let us start by walking you through the most basic usage of the stat command. For this, all we need to know is the name of the file you want to get the status of.
The syntax for the usage of this command is as simple as what we have shown below.
stat Command Values for a File
The stat command can provide you with a wealth of information about your files.
Below, we will quickly go over the fields presented by default.
File– The name of the file that the stats are about. This value is typically what you have passed into the command.
If it is a symbolic file, it will show the file name and what it is linked to.
Size– The size of the current file. This value is typically referenced in bytes.
Blocks– This is the number of blocks that this file consumes on the filesystem.
Even though a file might only consume 1 byte, it still consumes an entire block. If the block size is set to 4096 bytes, then that is how much space will be consumed on the drive.
IO Block– This value represents the size of a block. The block size is dependent on the filesystem.
File Type– The type of file that this file is. As everything on Linux is represented as a file, this could be one of the following values.
- regular file
- special file
- symbolic link
Device– The device number. This value is expressed in either decimal or hexadecimal format.
This is the ID corresponding to the drive that the files are stored on.
Inode– The inode value identifies this file within the current filesystem.
Combined with the device number, this creates a unique identifier for the file.
Links– The number of hard links that point to this file.
When used on a directory, this value will represent the number of files within it. This number will include the “
.” and “
..” entries. So even an empty directory will have two links.
Access– The file permissions that have been set for this file.
This value shows the permissions in both their octal and symbolic formats.
Uid– This shows both the owner’s User ID and account name.
Gid– This value represents the owner’s Group ID and group name.
Access– That timestamp indicates the last time a user accessed this file.
Modify– The timestamp represents when a user last modified the file’s contents.
Change– The last time a user modified the attributes or contents of the file.
For example, setting new permissions to a file will cause this value to be updated.
Birth– This timestamp is used to store when a user created the file. Not all Linux systems set this value.
You can adjust various bits of data shown here by using some simple commands. For example, you can use the touch command to modify the “
access” and “
Likewise, the chmod command would adjust the “
Access” value as it modifies the permission bits.
Example of Using the stat Command
To show you how this works, let us show an example of using the stat command on a file located at “
/etc/hostname“. This file may not be available on your Linux system, but it is included with distributions such as Debian and Ubuntu.
Run the following command on your device to retrieve the stats of the “
An example of the results you would see from using this command is the following. Your values, of course, will differ.
Using the stat Command on Multiple Files
The stat command on Linux allows you to retrieve the status of multiple files at once. You can list as many files as you want, and the stat utility will retrieve details for each of them.
Below you can see an example of what the command would look like if you were retrieving the status of two files.
stat FILENAME1 FILENAME2...
One thing to note is the stat command does support both the question mark (
?) and asterisk wildcard (
The question mark wildcard represents any single character. The asterisk can represent any string of characters.
Example of Using stat on Multiple Files
For this example, we will be using stat on two files. These files will be “
/etc/hostname” and “
All we need to do is use “stat” followed by our two filenames, as shown below.
stat /etc/hostname /etc/resolv.conf
Using this, the stat command will print out information about each file, one after another.
You can tell the start of a new file by paying attention to the filename next to “
Example of Using Wildcards in a Filename
By using wildcards, you can select multiple files easily. For example, if we wanted to use the stat command to check all “
.conf” files, you could use a command like the following.
Using the asterisk at the start of the string will automatically include files ending in “
Below is an example of the output you could receive when using a similar command.
Following Symbolic Links with the stat Command
If you use the stat command on a symbolic link file, it will only provide you with information about the link itself.
There are a couple of ways to tell that a file you are currently viewing is a symbolic link. The easiest way is to see if the “
File type” is specified as a “
You can also see what that symbolic link is linking to by paying attention to the “
File” value. First, you will see the filename you used stat on, followed by the file that it is linking to.
To get the stat utility to follow this symlink, the command an option called “
This option allows us to retrieve information about the file it links to rather than the symbolic link itself.
stat -L FILENAME
Example of Following a Symbolic Link
For this example, we have a symlink called “
test” that links to another file on our system. This file is the same example that we used in the screenshot above.
To get the status of the linked file itself, we only need to use “
stat“, the “
-L” option, and finally, our example filename.
stat -L test
Below you can see that we now have information about the file itself rather than the link that we referenced.
You will notice that the filename will show as the link and not of the file that is linked.
Using stat to Check the Filesystems Status
Additional functionality of the stat command is that it can check the filesystem’s status. You can use the “
--file-system) option to achieve this.
When used on a file or directory, it retrieves the filesystem’s status that it resides on.
stat -f FILENAME
stat Command Values for a Filesystem
Using the stat command to get the status of a filesystem will give you different information.
Below we will quickly go over the information provided through this utility for filesystems.
File– The name of the file that this status request is for.
ID– The ID of the filesystem that this file is on.
This value is displayed in hexadecimal notation.
Namelen– This value represents the maximum length for filenames on this filesystem.
Type– The type of filesystem this has been formatted as.
Block Size– Tells the system how many blocks should be read at one time for faster transfer speeds.
Fundamental Block Size– The actual size of each block on the filesystem.
Total– The total number of blocks within this filesystem.
Free– The number of free blocks remaining in the filesystem.
Available– This value shows the available free blocks that regular (non-root) users can utilize.
Total– The total count of inodes available in the filesystem.
Free– The number of free inodes available out of the total.
Example of Using stat to Check the Filesystem
For this example, let us check the filesystem’s status for the current directory by using a dot (
.) as the filename.
All you need to use is “
stat“, followed by the filesystem option “
-f“, then finally the dot as our filename.
stat -f .
Using this command, you will get the details about the filesystem where the current directory resides.
Getting a terse Report from the stat Command
The stat command can also produce a condensed report, excluding the headers shown in the normal output.
We need to use the “
--terse) option when calling the stat command to produce this terse report.
The syntax for using this command is as straightforward as what we have shown below.
Terse Report Information
When using the terse option, the information provided from the stat command can be hard to understand.
Below, we will walk through the two different formats that the terse report uses. Utilize these lists to understand the data that has been shown to you.
Terse Report Format for a File
When you use the “
terse” option on a file, it will produce a report with the following values.
This is equivalent to using the following custom format:
%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %W %o %C
- Filename (
- Total size (
- Number of blocks allocated (
- Raw mode, displayed in hex (
- User ID of the owner (
- Group ID of the owner (
- Device number, displayed in hex (
- inode number (
- Number of hard links (
- major device type in hex (
- minor device type in hex (
- Time of last access. Displayed as seconds since epoch (
- Time of last modification. Display as seconds since epoch (
- Time of last status change. Displayed as seconds since epoch (
- Time of file birth. Displayed as seconds since epoch (
- Optimal I/O transfer size hint (
- SELinux security context string (
Terse Report Format for a Filesystem
The format differs significantly when using the terse option alongside the filesystem option.
When used on a filesystem, the equivalent format is the following:
%n %i %l %t %s %S %b %f %a %c %d
- Filename (
- File system ID in hex (
- The maximum length of filenames (
- File system type in hex (
- Block size for faster transfers (
- Fundamental block size (
- Total Number of data blocks in the filesystem (
- Free blocks in the filesystem (
- Free blocks available to non-superusers (
- Total number of file nodes in the filesystem (
- Total number of free file nodes (
Example of Using terse on a File with the stat Command
For this example, we will generate a terse report for the file located at “
stat -t /etc/hostname
Below is an example of how the data would look once you have used the command. To understand all of this data, refer to our list above.
Example of Using the terse Option on a Filesystem
When using the terse option with the stat command on a filesystem, the returned data is slightly different.
We will use the current directories filesystem for our terse report for this example. All this requires us to use is the “
-t” option for the terse report, “
-f” to get the filesystem status, and finally the filename.
stat -t -f .
Below you can see the response you will get from using this command.
Customizing the Output of the stat Command
The stat command has the functionality for you to change the format of the outputted data. We can use the “
--printf” or “
--format” options to modify the default format.
These options achieve roughly the same thing. However, they function slightly differently. Therefore, we will go into both of these options separately.
Below, we will go through the formatting variables that you can use for both the “
--format” and “
These formatting variables differ based on whether you are using the stat command to get the status of a file or a filesystem.
Variables for Files
Refer to the table below to see the various variables you can use in your format string. Remember that these variables are strictly for a file.
When the “
--filesystem” option is utilized, you must refer to the “Variables for Filesystems” section.
|Permission bits, displayed in octal
|Permissions bits and file type, displayed in a human-readable form
|Number of blocks allocated to the file
|The size of each block, displayed in bytes
|SELinux Security Context String
|Device number displayed in decimal format
|Device number displayed in hex format
|Raw mode in hex
|Group ID of the owner
|Group Name of the owner
|Number of hard links
|Quoted file name with dereferencing if symbolic link
|Optimal I/O transfer size hint
|Total size, in bytes
|Major device type in hex, for character/block device special files
|Minor device type in hex, for character/block device special files
|User ID of the owner
|User name of the owner
|Time of the file’s birth. Displayed in a human-readable format. Will show as a hyphen (
-) if unknown
|Time of when the file was born. Displayed in seconds since epoch. It will be shown as a
0 if unknown
|Time of last access. Displayed in a human-readable format.
|Time of last access. Displayed as seconds since epoch.
|Time of last data modification. Displayed in a human-readable format.
|Time of last data modification. Displayed as seconds since epoch.
|Time of last status change. Displayed in a human-readable format.
|Time of last status change. Displayed as seconds since epoch.
Variables for Filesystems
Below you can see all the available variables when you utilize the “
-f” or “
|Free blocks available on the filesystem for non-superusers
|Total amount of data blocks available in the filesystem
|Total amount of file nodes in the filesystem
|The number of free file nodes available on the filesystem
|Free blocks available on the filesystem
|Filesystem ID displayed in hex
|Maximum Length of Filenames that are on this filesystem
|Block size for faster transfers
|Fundamental block size for block counts
|Filesystem type in hex
|Filesystem type in human-readable form
Customize using the Format Option
Using the “
--format” or “
-c” option is a relatively straightforward process and will allow you to display the data you want.
A new line will be automatically appended to the formatting string. However, you can’t insert any new lines within the format string using this option. Instead, everything will be output on the same line until it ends.
stat --format "FORMAT STRING" FILENAME
Example of Using the
--format Option with a Custom String
For this example, we will use the custom format string as shown below:
Name: %n, User: %U, Permissions: %A, Mount Point: %m
To showcase what sort of text this would produce, we will test it on the “
stat --format "Name: %n, User: %U, Permissions: %A, Mount Point: %m" /etc/hostname
Below you can see the result this custom format string would produce from the stat command.
The stat Command with the Printf Option
You can use the “
--printf” option with the stat command if you want support for backslash escapes. An example of this is adding new lines by using “
\n” within your format string.
Unlike the “
--format” option, this will not automatically add a trailing newline to the end of the request.
stat --printf "FORMAT STRING" FILENAME
Example of Using the
--printf Option with a Custom Format
In this example, we will be using a custom format with the following:
Name: %n\nUser: %U\nPermissions: %A\nMount Point: %m\n
Instead of using commas to separate each value like in our previous example, we will use the new line character “
stat --printf "Name: %n\nUser: %U\nPermissions: %A\nMount Point: %m\n" /etc/hostname
Using this command, you will end up with a result similar to the one shown below.
Hopefully, you will have learned how to use the stat command on a Linux system by this point in the guide.
The stat utility is useful for providing the status of a file or filesystem. The tool provides a wealth of information, such as the inode number, device number, permission bits, and even more.
If you have run into any issues using the stat command to get the information you need, leave a comment below.