Linux Commands

Linux Stat Command Examples

The stat command displays the detailed summary of the given file or file system. We will show you in this article the basic use of the stat command with different examples.

Syntax of Stat Command

The basic syntax of the Linux stat command is given below:

$ stat [options] [file-name]

More than one file name and options can pass an argument with the stat command that controls the command behavior and results.

Display Linux File Status Using Stat Command

When only given the file name with the stat command, the complete information about file status will display on the terminal. For example, the following file detail can be displayed by using the state command:

$ stat test_file.txt

Display the Information About the File System

To display the information about system status instead of a file means to check the system status in which the given file resides. In this case, use the option “-f” with the file name which is given below:

$ stat -f test_file.txt

The following details about the system status will show on the terminal:

You can also pass the directory or filesystem as an argument instead of passing the file name, which is shown in the following screenshot:

Enable or Follow the Symbolic Links

As we know, Linux supports symbolic links (symlink). When you use the stat command on a symlink, it will display the following about the symlink, but it doesn’t provide the information about the file it points to. You can review the following screenshot:

$ stat /etc/resolv.conf

To follow or enable the symlinks, use the “-L” option with the stat command as follows:

$ stat -L /etc/resolv.conf

So, by using the above command, the symlink and file information will display on the terminal.

Display Output in a Customized Format

Two different options are available that can be used with the stat command to format the output: -c, –printf=”format” and (–format=”format”).

The –format automatically inserts a new line after each operand’s result. The –printf option interprets the backslash and escape sequences.

Let’s explain with an example. Type the following command to try the –printf option:

# stat --printf='%U\n%G\n%z\n' test_file.txt

The meaning of the format sequences, which are used in the above command given below:

%U – Display the owner name like ‘kbuzdar’

%G – used for group’s owner name such as ‘kbuzdar’

%z – display the time of last change in status in human-readable format

There are more sequences you can use with the –printf option according to your needs.

Display Output in the Terse Format

Using the stat command, you can display the details in the terse format that is mostly useful for parsing. Type the option “-t” or –terse with the stat command to display the information in the terse format as follows:

# stat -t test_file.txt

To explore more options about the stat command, read the manual pages of the stat command by running the following terminal command:

$ man stat

Or

$ stat --help

Conclusion

This article gives a brief information about the stat command. We have explained all the basic and important options in this article that will hopefully help you.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.