How To Use Stat Command In Linux.
This article explains how to use stat command.
stat is a command-line utility that displays detailed information about given files or file systems.
Using the stat Command
The syntax for the stat command is as follows:
stat accepts one or more input FILE names and includes a number of options that control the command behavior and output.
Let’s take a look at the following example:
The output will look something like this:
When invoked without any options, stat displays the following file information:
File – The name of the file.
Size – The size of the file in bytes.
Blocks – The number of allocated blocks the file takes.
IO Block – The size in bytes of every block.
File type – (ex. regular file, directory, symbolic link.)
Device – Device number in hex and decimal.
Inode – Inode number.
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 – The last time the file was accessed.
Modify – 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 information about the file system where the given file resides, instead of information about the file itself, use the -f, (–file-system) option:
The output of the command will look like this:
When stat is invoked with the -f option, it shows the following information:
File – The name of the file.
ID – File system ID in hex.
Namelen – Maximum length of file names.
Fundamental block size – The size of each block on the file system.
Total – Number of total blocks in the file system.
Free – Number of free blocks in the file system.
Available – Number of free blocks available to non-root users.
Total – Number of total inodes in the file system.
Free – Number of free inodes in the file system.
Dereference (Follow) Symlinks
By default, stat does not follow symlinks . If you run the command on a symlink the output will include information about the symlink, not the file it points to:
To dereference (follow) the symlink and display information about the file to which the symlink points, use the -L, (–dereference) option:
Customizing the Output
The stat command has two options that allow you to customize the output according to your needs: -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.
There are many format directives for files and file systems that can be used with –format and –printf.
For example, to view only the type of the file, you would run:
You can combine any number of formatting directives and optionally use custom separators between them. The separator can be a single character or a string:
To interpret special characters like newline or tab, use the –printf option:
\n prints a new line:
The stat can also display the information in the terse form. This format is useful for parsing by other utilities.
Invoke the command with -t (–terse) option to print the output in the terse form:
For a complete list of all format directives for files and file systems type, man stat or stat –help in your terminal.
The stat command prints information about given files and file systems.
In Linux, several other commands can display information about given files, with ls being the most used one, but it shows only a chunk of the information provided by the stat command.