Using the stat Command on Linux

This tutorial will walk you through how you can use the stat command on your Linux system.

stat command on Linux

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

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”-L“.

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 FILENAME

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
    • directory
    • 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 “modify” times.

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 “/etc/hostname“.

stat /etc/hostname

An example of the results you would see from using this command is the following. Your values, of course, will differ.

stat Command on /etc/hostname Example Result

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 (*) characters.

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 “/etc/resolv.conf“.

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 “File“.

Example of using stat on multiple files

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 “.conf“.

stat /etc/*.conf

Below is an example of the output you could receive when using a similar command.

stat command using the wildcard

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 “symbolic link“.

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.

stat command used on a symbolic link

To get the stat utility to follow this symlink, the command an option called “-L” (“--dereference“) .

This option allows us to retrieve information about the file it links to rather than the symbolic link itself.

stat -L FILENAME

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.

Follow symbolic link using the stat command

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 “-f” (--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.
  • Blocks:
    • 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.
  • Inodes:
    • 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.

Using the stat command to get status of filesystem

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 “-t” (--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.

stat-t FILENAME

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
  1. Filename (%n)
  2. Total size (%s)
  3. Number of blocks allocated (%b)
  4. Raw mode, displayed in hex (%f)
  5. User ID of the owner (%u)
  6. Group ID of the owner (%g)
  7. Device number, displayed in hex (%D)
  8. inode number (%i)
  9. Number of hard links (%h)
  10. major device type in hex (%t)
  11. minor device type in hex (%T)
  12. Time of last access. Displayed as seconds since epoch (%X)
  13. Time of last modification. Display as seconds since epoch (%Y)
  14. Time of last status change. Displayed as seconds since epoch (%Z)
  15. Time of file birth. Displayed as seconds since epoch (%W)
  16. Optimal I/O transfer size hint (%o)
  17. SELinux security context string (%C)

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
  1. Filename (%n)
  2. File system ID in hex (%i)
  3. The maximum length of filenames (%l)
  4. File system type in hex (%t)
  5. Block size for faster transfers (%s)
  6. Fundamental block size (%S)
  7. Total Number of data blocks in the filesystem (%b)
  8. Free blocks in the filesystem (%f)
  9. Free blocks available to non-superusers (%a)
  10. Total number of file nodes in the filesystem (%c)
  11. Total number of free file nodes (%d)

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 “/etc/hostname“.

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.

Generate a terse report using the stat command

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.

stat command terse report for a filesystem

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.

Formatting Variables

Below, we will go through the formatting variables that you can use for both the “--format” and “--printf” options.

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.

VariableDescription
%aPermission bits, displayed in octal
%APermissions bits and file type, displayed in a human-readable form
%bNumber of blocks allocated to the file
%BThe size of each block, displayed in bytes
%CSELinux Security Context String
%dDevice number displayed in decimal format
%DDevice number displayed in hex format
%fRaw mode in hex
%FFile Type
%gGroup ID of the owner
%GGroup Name of the owner
%hNumber of hard links
%iinode number
%mMount point
%nFilename
%NQuoted file name with dereferencing if symbolic link
%oOptimal I/O transfer size hint
%sTotal size, in bytes
%tMajor device type in hex, for character/block device special files
%TMinor device type in hex, for character/block device special files
%uUser ID of the owner
%UUser name of the owner
%wTime of the file’s birth. Displayed in a human-readable format. Will show as a hyphen (-) if unknown
%WTime of when the file was born. Displayed in seconds since epoch. It will be shown as a 0 if unknown
%xTime of last access. Displayed in a human-readable format.
%XTime of last access. Displayed as seconds since epoch.
%yTime of last data modification. Displayed in a human-readable format.
%YTime of last data modification. Displayed as seconds since epoch.
%zTime of last status change. Displayed in a human-readable format.
%ZTime 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 “--filesystem” option.

VariablesDescription
%aFree blocks available on the filesystem for non-superusers
%bTotal amount of data blocks available in the filesystem
%cTotal amount of file nodes in the filesystem
%dThe number of free file nodes available on the filesystem
%fFree blocks available on the filesystem
%iFilesystem ID displayed in hex
%lMaximum Length of Filenames that are on this filesystem
%nFile Name
%sBlock size for faster transfers
%SFundamental block size for block counts
%tFilesystem type in hex
%TFilesystem 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 “/etc/hostname” file.

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.

stat command using the format option

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 “\n“.

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.

stat command on linux and using the printf option

Conclusion

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.

Be sure also to check out our many other Linux command guides and tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *