Linux Stat Command Examples

42

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:

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:

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

linux stat command examples Linux Stat Command Examples 1622088345 728 Linux Stat Command Examples

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

linux stat command examples Linux Stat Command Examples 1622088345 295 Linux Stat Command Examples

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:

linux stat command examples Linux Stat Command Examples 1622088346 278 Linux Stat Command Examples

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.

linux stat command examples Linux Stat Command Examples 1622088347 609 Linux Stat Command Examples

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=”%Un%Gn%zn” test_file.txt

linux stat command examples Linux Stat Command Examples 1622088347 467 Linux Stat Command Examples

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:

linux stat command examples Linux Stat Command Examples 1622088348 571 Linux Stat Command Examples

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

Or

linux stat command examples Linux Stat Command Examples 1622088348 699 Linux Stat Command Examples

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.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More