Linux Stat Command Explained

The stat command is used in Linux/Unix system to display detailed information about files and file systems. It is most commonly used to get file timestamps.
The Linux ls command usually gives you basic details about a file whereas stat command prints additional information about the file pulled from the inode.
This article explains how to use stat command.

Using the stat Command

The syntax for the stat command is as follows:

stat [OPTION]... FILE...

stat accepts one or more input FILE names and includes a number of options that control the command behavior and output.

Let’s take a look at the following example:

To display a file status such as size, inode number links, and file timestamps, run:

$ stat instrument.ino 
  File: instrument.ino
  Size: 4052      	Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d	Inode: 4201227     Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/   rasho)   Gid: ( 1000/   rasho)
Access: 2021-01-31 22:28:29.529792705 +0100
Modify: 2021-01-31 22:36:27.407930430 +0100
Change: 2021-01-31 22:36:27.407930430 +0100
 Birth: -

When invoked without any options, stat displays the following file information:

Check filesystem status

To prints out the filesystem status on which the file resides instead of giving information about the regular file, use -f or --file-system option.
For example:

$ stat -f instrument.ino 
  File: "instrument.ino"
    ID: 19600ab60794303 Namelen: 255     Type: ext2/ext3
Block size: 4096       Fundamental block size: 4096
Blocks: Total: 28584489   Free: 15375618   Available: 13912834
Inodes: Total: 7299072    Free: 6494484

When stat is invoked with the -f option, it shows the following information:

When stat is invoked with the -f option, it shows the following information:

To follow symlinks

The stat command does not follow symlinks by default. When you run it on a symlink, the output comprises information about the symlink but not the file it points to.

$ stat /usr/share/zoneinfo/Europe/Belgrade
File: /usr/share/zoneinfo/Europe/Belgrade
Size: 1948 Blocks: 8 IO Block: 4096 regular file
Device: 802h/2050d Inode: 3818409 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2021-01-31 23:00:27.342347034 +0100
Modify: 2021-01-08 17:19:33.000000000 +0100
Change: 2021-01-21 19:20:00.774778445 +0100
Birth: -

To follow the symlink and print out information about the file it points to, use the -L option as shown:

$ stat -L /usr/share/zoneinfo/Europe/Belgrade 
  File: /usr/share/zoneinfo/Europe/Belgrade
  Size: 1948      	Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d	Inode: 3818409     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-01-31 23:00:27.342347034 +0100
Modify: 2021-01-08 17:19:33.000000000 +0100
Change: 2021-01-21 19:20:00.774778445 +0100
 Birth: -

Customizing the Output

The stat command has two options that allow you to customize the output according to your needs: -c, (--format="format") and --printf="format".
The difference between these two options is that when two or more files are used as operants --format automatically adds a newline after each operand’s output. The --printf interprets backslash escapes.
There are many format directives for files and file systems that can be used with --format and --printf.
For example, to view only the type of the file, you would run:

$ stat --format="%F" /dev/null

You can combine any number of formatting directives and optionally use custom separators between them. The separator can be a single character or a string:

$ stat --format="%n,%F" /dev/null

To interpret special characters like newline or tab, use the --printf option:

$ stat --printf='Name: %n\nPermissions: %a\n' /etc

\n prints a new line:

Name: /etc
Permissions: 755

Conclusion

The stat is a handy command to check file timestamps such as file modification or acess time. In this guide, we have covered the stat command in Linux and highlighted a few example usages.

Exit mobile version