A stat command displays information about a file or a file system. With the stat command, you can get information like the file size, its permissions, the IDs of the group and user that have access, and the date and time that the file was created. Another feature of the stat command is that it can also provide information about the file system. When we want to know the information about a file, we should use this tool. So in this blog, you will get to know about the Stat command in Linux with appropriate examples.
Stat Command in Linux
Stat command is useful when you want to know everything about a specific file. The following is the basic syntax for using stat:
stat --options filenames
Here is the list of the information we get after executing the stat command in Linux:
Name | Description |
File | It represents the name of the provided file. |
File type | It represents the type of file, for e.g., special files, directories, regular files or symbolic links, etc. |
ID | It shows the file system ID in hexadecimal format. |
Birth | It shows the time at which the file was created. |
Size | It shows the size of that particular file (in bytes) |
Device | It shows the device number (in the hexadecimal format) |
Blocks | It displays the total number of hard disk blocks used to store the file. |
IO Block | The size of each allocated block (in bytes) |
Modify | It shows the last modification-related details. |
Context | It shows the stored SELinux security context. |
Inode | It shows the Inode number of the file. |
Change | It shows the information about the time when a file’s attribute or content was last altered. |
Access | It represents all access permissions (in the numeric and symbolic methods) |
Links | It shows the total number of hard links of the file. |
Useful Example of Stat Command in Linux
Now we will explain different examples and usage of stat command in Linux. We will use the Pyload.txt file in the example, which is located in the Documents directory.
1. Display the File System Related Information
Stat command provides brief information about the file, but if you want to see the information about the file system instead of the file, please use the -f option.
stat -f Pyload.txt
If you don’t use the -f option in the stat, then you will get the result like this:
stat Pyload.txt
You can use the -t option to print all of the information in the terse format. So here is the command for the terse form:
stat -t ~/Documents/Pyload.txt
2. Dereference Symlinks
Use the -L, (–dereference) option to dereference the symlink and display information about the file to which it points:
stat -L ~/Documents/Pyload.txt
As a default, the stat command in Linux does not follow symlinks. You will get information about the symlink, not the file that it points to, if you run the command on a symlink:
stat ~/Documents/Pyload.txt
3. Stat Command for Multiple Files
You can use the stat command to get the information about multiple files. Here we are using the following command to get information about Pyload.txt and Ubuntupit.tar files.
stat Pyload.txt Ubuntupit.tar
4. Customize the Output of Stat Command
The stat command offers two different options for customizing the output as per your requirements, and these options are:
-c, (–format=”format”)
–printf=”format”.
The major difference between the above options is that the --format
automatically adds a newline after every operand output when you use two or more files as operants.
stat --format='%n' ~/Documents/Pyload.txt
stat --format='%n'\n ~/Documents/Pyload.txt
Using this command, you can view only the type of file:
stat --format="%F" ~/Documents/Pyload.txt
Using custom separators between formatting directives allows you to combine any number of formatting directives. For example:
stat --format="%n,%F" ~/Documents/Pyload.txt
For interpreting the special characters like newline or tab, use the –printf option:
stat --printf='Name: %n\nPermissions: %a\n' ~/Documents/Pyload.txt
You can use the below command to get the specific results:
stat --printf='%U\n%G\n%C\n%z\n' ~/Documents/Pyload.txt
Here:
- %U: It shows the username of the owner
- %G: It shows the group name of the owner
- %C: It shows the context string of SELinux security
- %z: It shows the time when the last changes occurred (in the human-readable format).
With the below command, the system shows the different format sequences for the file system:
stat --printf='%n\n%a\n%b\n' ~/Documents/Pyload.txt
- %n: It shows the name of the file
- %a: Prints the free blocks that are available to non-superusers
- %b: It shows the total data blocks in a file system
If you want to get the complete list of the format directives, then please execute the below command:
man stat
Finally, Insight
Using the stat command, you can print information about files and file systems. Sometimes it works as a replacement of Stat as it also displays information about given files. So if you want to get more information about the ls command, then please visit our official website.