stat command is a useful utility for viewing file or file system status. It retrieves information such as file type; access rights in octal and human-readable; SELinux security context string; time of file birth, last access, last data modification, last status change in both human-readable and in seconds since Epoch, and much more.
It has an option to specify a custom format instead of the default, for displaying information. In this guide, we will look at five stat command examples for Linux newbies.
Check Linux File Status
1. The easiest way to use stat is to provide it a file as an argument. The following command will display the size, blocks, IO blocks, file type, inode value, number of links and much more information about the file /var/log/syslog, as shown in the screenshot:
$ stat /var/log/syslog File: '/var/log/syslog' Size: 26572 Blocks: 56 IO Block: 4096 regular file Device: 80ah/2058d Inode: 8129076 Links: 1 Access: (0640/-rw-r-----) Uid: ( 104/ syslog) Gid: ( 4/ adm) Access: 2018-04-06 09:42:10.987615337 +0530 Modify: 2018-04-06 11:09:29.756650149 +0530 Change: 2018-04-06 11:09:29.756650149 +0530 Birth: -
Check File System Status
2. In the previous example, stat command treated the input file as a normal file, however, to display file system status instead of file status, use the
$ stat -f /var/log/syslog File: "/var/log/syslog" ID: ce97e63d2201c974 Namelen: 255 Type: ext2/ext3 Block size: 4096 Fundamental block size: 4096 Blocks: Total: 84769790 Free: 16012830 Available: 11700997 Inodes: Total: 21544960 Free: 20995459
You can also provide a directory/filesystem as an argument as shown.
$ stat -f / File: "/" ID: ce97e63d2201c974 Namelen: 255 Type: ext2/ext3 Block size: 4096 Fundamental block size: 4096 Blocks: Total: 84769790 Free: 16056471 Available: 11744638 Inodes: Total: 21544960 Free: 21005263
Enable Following of Symbolic Links
3. Since Linux supports links (symbolic and hard links), certain files may have one or more links, or they could even exist in a filesystem.
To enable stat to follow links, use the
-L flag as shown.
$ stat -L / File: '/' Size: 4096 Blocks: 8 IO Block: 4096 directory Device: 80ah/2058d Inode: 2 Links: 25 Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2018-04-09 10:55:55.119150525 +0530 Modify: 2018-02-20 11:15:54.462893167 +0530 Change: 2018-02-20 11:15:54.462893167 +0530 Birth: -
Use a Custom Format To Display Information
4. stat also allows you to use a particular or custom format instead of the default. The
-c flag is used to specify the format used, it prints a newline after each use of format sequence.
Alternatively, you can use the
--printf option which enables interpreting of backslash escapes sequences and turns off printing of a trailing newline. You need to use
\n in the format to print a new line, for example.
# stat --printf='%U\n%G\n%C\n%z\n' /var/log/secure
Meaning of the format sequences for files used in above example:
- %U – user name of owner
- %G – group name of owner
- %C – SELinux security context string
- %z – time of last status change, human-readable
5. Here is an example which shows using of accepted format sequences for file systems.
$ stat --printf='%n\n%a\n%b\n' /
Meaning of the format sequences used in the above command.
- %n – shows the file name
- %a – print free blocks available to non-superuser
- %b – outputs total data blocks in file system
Print Information in Terse Form
-t option can be used to print the information in terse form.
$ stat -t /var/log/syslog /var/log/syslog 12760 32 81a0 104 4 80a 8129076 1 0 0 1523251873 1523256421 1523256421 0 4096
As a last note, your shell may have its own version of stat, please refer to your shell’s documentation for details about the options it supports. To see all accepted output format sequences, refer to the stat man page.
$ man stat
In this article, we have explained five stat command examples for Linux newbies. Use the feedback form below to ask any questions.