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.
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:
- File – The name of the file.
- Size – The size of the file in bytes.
- Blocks – The number of allocated blocks the file takes.
- IO Block – The size in bytes of every block.
- File type – (ex. regular file, directory, symbolic link.)
- Device – Device number in hex and decimal.
- Inode – Inode number.
- Links – Number of hard links.
- Access – File permissions in the numeric and symbolic methods.
- Uid – User ID and name of the owner .
- Gid – Group ID and name of the owner.
- Context – The SELinux security context.
- Access – The last time the file was accessed.
- Modify – The last time the file’s content was modified.
- Change – The last time the file’s attribute or content was changed.
- Birth – File creation time (not supported in Linux).
Check filesystem status
To prints out the filesystem status on which the file resides instead of giving information about the regular file, use
$ 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:
stat is invoked with the
-f option, it shows the following information:
- File – The name of the file.
- ID – File system ID in hex.
- Namelen – Maximum length of file names.
- Fundamental block size – The size of each block on the file system.
- Total – Number of total blocks in the file system.
- Free – Number of free blocks in the file system.
- Available – Number of free blocks available to non-root users.
- Total – Number of total inodes in the file system.
- Free – Number of free inodes in the file system.
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:
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
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
$ stat --printf='Name: %n\nPermissions: %a\n' /etc
\n prints a new line:
Name: /etc Permissions: 755
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.