The stat is a command-line utility that shows detailed information about given files or file systems. In this article we will show you how to use stat command.
Using the stat Command
Following is the basic syntax for the stat
command:
stat [OPTION]... FILE...
You can pass multiple input FILE
names and pass multiple options along with the stat
command.
Let’s take a look at the following example:
stat readme.md
It will show the output as following:
File: readme.md
Size: 3924 Blocks: 8 IO Block: 4096 regular file
Device: fc01h/64513d Inode: 276385 Links: 1
Access: (0775/-rwxrwxr-x) Uid: ( 1000/ tecnstuff) Gid: ( 1000/ tecnstuff)
Access: 2019-12-07 12:00:58.732443339 +0000
Modify: 2019-09-18 13:05:37.447485838 +0000
Change: 2019-09-18 13:05:37.447485838 +0000
Birth: -
If the stat
command used without any options, it will show following file information:
File
– Name of the file.Size
– Size of the file in bytes.Blocks
– The number of allocated blocks the file takes.IO Block
– The size of every block in bytes.File type
– Shows file type like regular file, directory, symbolic link, etc.Device
– Device number in hex and decimal.Inode
– Number of Inode.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
– Shows the last time the file was accessed.Modify
– Displays when 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)
Displaying Information About the File System
To get the information about the file system where the file located, you should use -f
, (--file-system
) option:
stat -f file.txt
It should show the output as following:
File: "readme.md"
ID: 55dedc5188bf2d0a Namelen: 255 Type: ext2/ext3
Block size: 4096 Fundamental block size: 4096
Blocks: Total: 6306740 Free: 5432885 Available: 5428789
Inodes: Total: 3225600 Free: 3087260
The stat
command will show the following information when used with the -f
option:
File
– Name of the file.ID
– Shows File system ID in hex.Namelen
– Maximum length of file names.Fundamental block size
– The size of each block on the file system.Blocks
:Total
– Display number of total blocks in file system.Free
– Shows free blocks in file system.
Available
– Number of free blocks available to non-root users.Inodes
:Total
– Total inodes in file system.Free
– Number of free inodes in file system.
Dereference (Follow) Symlinks
Symlinks are not followed by the stat command. If you try to run stat
command on a symlink, it wil not show the information about the file where points but it shows the details of symlink.
stat /etc/resolv.conf
File: /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf
Size: 39 Blocks: 0 IO Block: 4096 symbolic link
Device: fc01h/64513d Inode: 828 Links: 1
Access: (0777/lrwxrwxrwx) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2020-09-05 07:30:10.034929613 +0000
Modify: 2019-08-13 16:31:25.030495647 +0000
Change: 2019-08-19 16:50:49.563281570 +0000
Birth: -
To view the file information where the symlink is pointed, you should use the -L
(--dereference
) option with stat command:
stat -L /etc/resolv.conf
File: /etc/resolv.conf
Size: 715 Blocks: 8 IO Block: 4096 regular file
Device: 17h/23d Inode: 487 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 101/systemd-resolve) Gid: ( 103/systemd-resolve)
Access: 2020-09-05 02:00:55.240523831 +0000
Modify: 2020-09-02 13:38:19.768000000 +0000
Change: 2020-09-02 13:38:20.464000000 +0000
Birth: -
Customizing the Output
You can customize the output of the stat command using these two options: -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.
You can use many format directives for files and file systems to use with --format
and --printf
.
For example, to view only the type of the file, type:
stat --format="%F" /dev/null
character special file
You also can combine the formatting directives and also use the custom separators between them. The separator can be a single character or a string:
stat --format="%n,%F" /dev/null
/dev/null,character special file
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
The stat can also display the information in terse form. This format is useful for parsing by other utilities.
Invoke the command with -t
(--terse
) option to print the output in terse form:
stat -t /etc
/etc 4096 8 41ed 0 0 fc01 206 107 0 0 1599053870 1599053866 1599053866 0 4096
For a complete list of all format directives for files and file systems type man stat or stat --help
in your terminal.
Conclusion
The stat command prints information about given files and file systems. You also can get the file information using ls command in Linux systems.
If you have any questions or suggestion, leave a comment below.