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. In this article, we will explain how to use the stat command to get more detailed information about file systems.
STAT [OPTION] [FILE]
The following are arguments that can be used along with the stat command:
-f : Prints the status of the filesystem
-c –format=FORMAT: Displays the output in a specified format
-L, –dereference: used to follow symlinks
-t, –terse: used to display file information in terse form
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:
File – This is the name of the file being displayed.
Size – gives the size of the file in bytes.
Blocks – specifies the number of allocated blocks a file takes
IO Block – specifies the size of every block in bytes
File type – the type of the file
Device -Displays the device number decimal and hex
Inode – displays the inode number.
Links -specifies the number of hard links available.
Access – displays the file permissions in the numeric and symbolic forms.
UID – specifies the user ID
GID – specifies the group ID and owner.
Context – Specifies the SELinux security context.
Access -this is the last time the file was accessed.
Modify – shows the last time the file was modified.
Change – The last time the file’s attribute was changed.
Birth – Time the file was created
Displaying 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:
File – this is the name of the file.
ID – Displays the ID of the file system in hex.
Namelen – maximum file length
Fundamental block size – Defines the size of each block in the file system.
- Total – Total number of blocks in the file system.
- Free – specifies the number of free blocks in the file system.
- Available – specifies the number of free blocks available to non-root users.
- Total – specifies the total number of inodes available in the file system.
- Free – specifies the number of free inodes available.
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. In the example below, let’s print the device and inode number:
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
Display information in terse form
Use the -t option to display file information in terse form.
$ stat -t file.txt
The stat command gives important information about filesystems. In this article, we have covered how to use the Linux stat command.