Lesson 14: stat Command
In this lesson, you'll learn how to use the stat command to view file and filesystem status in Linux.
The stat command is a useful utility for viewing file or filesystem status by retrieving information such as file type, access rights in octal and human-readable format, 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.
Let's discuss some examples of the stat command to understand its working.
stat Command Syntax
stat [OPTIONS] FILE
stat Command Options
| Option | Description |
|---|---|
-f |
Display filesystem status instead of file status |
-L |
Follow symbolic links |
-c |
Use a specified format instead of the default |
--printf |
Use a specified format with backslash escape interpretation |
-t |
Print information in terse form |
1. Check Linux File Status
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 below:
$ stat /var/log/syslog
File: /var/log/syslog
Size: 522201 Blocks: 1024 IO Block: 4096 regular file
Device: 803h/2051d Inode: 1315423 Links: 1
Access: (0640/-rw-r-----) Uid: ( 104/ syslog) Gid: ( 4/ adm)
Access: 2023-09-28 12:38:24.944021741 -0400
Modify: 2023-09-29 16:39:38.499552808 -0400
Change: 2023-09-29 16:39:38.499552808 -0400
Birth: 2023-09-28 12:38:24.944021741 -0400
2. Check File System Status
In the previous example, the stat command treated the input file as a normal file. However, to display filesystem status instead of file status, use the -f option.
$ stat -f /var/log/syslog
File: "/var/log/syslog"
ID: b50950c5ab34acc9 Namelen: 255 Type: ext2/ext3
Block size: 4096 Fundamental block size: 4096
Blocks: Total: 12692608 Free: 9577979 Available: 8925128
Inodes: Total: 3244032 Free: 3021836
You can also provide a directory/filesystem as an argument, as shown.
$ stat -f /
File: "/"
ID: b50950c5ab34acc9 Namelen: 255 Type: ext2/ext3
Block size: 4096 Fundamental block size: 4096
Blocks: Total: 12692608 Free: 9577979 Available: 8925128
Inodes: Total: 3244032 Free: 3021836
3. Enable Following of Symbolic Links
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: 803h/2051d Inode: 2 Links: 20
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2023-09-29 16:09:32.551761465 -0400
Modify: 2023-07-19 14:24:06.042531361 -0400
Change: 2023-07-19 14:24:06.042531361 -0400
Birth: 2023-07-19 14:11:42.000000000 -0400
4. Use a Custom Format to Display Information
The stat command 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 the format sequence.
Alternatively, you can use the --printf option that enables the interpretation of backslash escape 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
root
adm
system_u:object_r:var_log_t:s0
2023-09-29 16:39:38.499552808 -0400
Meaning of the format sequences for files used in the above example:
%U- user name of the owner%G- group name of owner%C- SELinux security context string%z- time of last status change, human-readable
5. Use Format Sequences for File Systems
Here is an example that shows the use of accepted format sequences for file systems.
$ stat --printf='%n\n%a\n%b\n' /
/
8925128
12692608
Meaning of the format sequences used in the above command:
%n- shows the file name%a- print free blocks available to non-superusers%b- outputs total data blocks in the filesystem
6. Print Information in Terse Form
The -t option can be used to print the information in terse form.
$ stat -t /var/log/syslog
/var/log/syslog 522280 1024 81a0 104 4 803 1315423 1 0 0 1695919104 1696019988 1696019988 1695919104 4096
Note: Your shell may have its own version of stat , so 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.
That's all about the stat command for now!
Summary
The stat command is a powerful utility for retrieving detailed metadata about files and filesystems in Linux. It goes far beyond what ls -l provides, giving you inode numbers, timestamps, block sizes, SELinux context, and more.
Here's a quick recap of what we covered:
- Use
stat filenameto display complete metadata for a file. - Use
stat -f filenameto display filesystem status instead of file status. - Use
stat -Lto follow symbolic links when retrieving file information. - Use
stat -c FORMATto display output in a custom format. - Use
stat --printfto use a custom format with backslash escape interpretation. - Use
stat -t filenameto print information in terse form.
For a full list of available options, run:
stat --help
man stat