Linux Stat Command Examples
- Linux Stat Command Examples
The good old ls command is a favorite tool for listing files or directories contained in a directory. It can also list attributes such as file permissions, ownerships, file & directory size and modification date. But that is as far as it can go.
If you want more detailed information about a file, use the stat command. The stat command provides detailed information about a particular file or file system.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform Linux Systems queries.
In this context, we shall look into how to use the stat command to get more detailed information about file systems.
Th command for stat is given below:
$ STAT [OPTION] [FILE]
The following are arguments that can be used along with the stat command:
Stat command without arguments
In its basic form, the stat command can be used to check the information of a file, as shown:
$ stat [ file]
When no argument is passed to stat it displays the following information:
How to use stat to display information about the file system ?
Additionally, you can display information about a mount point or filesystem as shown.
$ stat -f /opt
When the option -f is passed to the stat command, it displays the following information:
How to Customize Output with stat command ?
The stat command allows you to customize the output. Use the –printf or –format options to display specific information.
For example, to print only the number of hard links available in a file use the %h format sequence. It automatically prints the output on a new line in cases where you have specified several options:
$ stat --format=%h [FILE]
To print the file type information:
$ stat --format=%F [FILE]
When using –printf, you need to use \n to print in a new line when displaying multiple operands.
How to use stat command to Enable or follow symlinks ?
When we run the stat command against a symlink it only provides information about the link. It does not provide information about the file that the link points to. Here's an example.
$ stat /etc/resolv.conf
Use the -L option to follow symlinks and get the information about the file it points to:
$ stat -L /etc/resolv.conf
How to Display information in terse form using stat command ?
Use the -t option to display file information in terse form.
$ stat -t file.txt
This article covers how to use the Linux stat command. Basically, The stat command gives important information about filesystems. 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. Also, It has an option to specify a custom format instead of the default, for displaying information.