Signup/Sign In

Stat Command In Linux

Stat Command

The stat is a command which delivers information about the file and filesystem. Stat command delivers information such as the size of the file, access permissions and the user ID and group ID, birth time access time of the file. Stat command has the additional capability, by which it may also report the file system information. This is the ideal tool to utilize when we want the details of any file.

Now let's examine one by one how can we utilize the stat command.

Using stat command

The fundamental syntax of using stat command is as follows:

stat —options filenames

Stat may take one or more filenames as an input to the stat command. Now let’s see one example

stat /etc/resolv.conf

Output:

Following is the information we obtain about the file when we execute the stat command.

  • File: The name of the given file. If the specified file is a symlink, then the name will be changed.
  • Size: The size of a particular file in Bytes.
  • Blocks: Total number of allotted blocks to the file to store on the hard drive.
  • IO Block: The size of every allocated block in bytes.
  • File type: The file may be of the following types: Regular files, special files, directories, or symbolic links.
  • Device: Device number in hexadecimal format.
  • Inode: Inode number of the file.
  • Links: Number of hard links of the file.
  • Access: Access permissions in the numeric and symbolic approaches.
  • Context: The field holds SELinux security context.
  • Access: The last time at which the file was accessed.
  • Modify: The last time at which file was edited.
  • Change: The last time the at which file’s attribute or content was modified.
  • Birth: The time at which the file was produced.

Now let’s explore how can we present information about the file system.

Displaying the File System Information

Stat command may offer the file system information when we supply file name with the -f (–file-system) parameters. Syntax of using the -f option with the stat command:

stat -f filename

Here is one example of the -f option :

We may also supply the directory or file system as an argument to the stat command as follows:

stat -f /

The information we obtain for the filesystem from the stat

  • File: The name of the given file.
  • ID: File system ID in hexadecimal format.
  • Namelen: The maximum length (number of characters) of a file name.
  • Fundamental block size: Total size of each block in the file system.
  • Total: Total number of blocks in the file system
  • Free: Total amount of free blocks in the file system
  • Available: Total amount of free blocks available for non-root users
  • Inodes: Total number of inodes in the file system.
  • Free: Total number of free inodes in the file system.

Using Stat With Multiple File

To acquire the information about several files using the stat command, simply say the filenames separated by space:

stat locale.conf login.defs

This will reveal the information about the specified files.

Dereference Symlinks

If we pass the symbolic link file to the stat command, then it will offer the information about the symbolic link instead of the actual file. Here is one example:

Now let’s explore how we may dereference Symlinks. To follow or dereference the symbolic and show information about the file of which the symbolic like point stat supply -L (–dereference) option. We may use this option to dereference the symbolic connections. Here Example:

stat -L /etc/localtime

We can see that stat is giving information about the /bin folder.

Customizing the Output

Stat allows us the opportunity to personalize output instead of utilizing the default output format. There are two techniques by which we may tailor the output

The first is using the -c option or the –format argument with the format of the output. If we pass the multiple files to the stat command with the –format option, then it will automatically append the new line after each operand’s output:

stat —format=' percent n' /etc/passwd

Another technique to print customized output is using the –print option with the format. With this option, we can offer trailing characters to the format for example if we want to add a new line after each line is produced we may use \n trailing characters with a backslash. Here is one example utilizing –print option

stat —print=' percent n\n' /etc/passwd

To know all format sequences, read the man page of the stat using the man command. The stat may also present the information in a terse manner. We may show information in a terse manner using -terse option:

stat -t /etc/passwd

Understanding the Timestamps

Timestamps show the information about the timezone. Generally, the timestamps represent the time at which the file was produced from the UTC timezone. if the value is positive, then the file was produced ahead of the UTC timezone and if the value is negative then the file is generated before the UTC timezone. For example:

In the preceding example, the value of the timestamp is +0530 which signifies the file is generated 5 hours 30 minutes ahead of UTC time. Here is one remark about stat command, your shell may have its own version of the stat.