stat command in Linux

We offer you a brighter future with FREE online courses - Start Now!!

This article will teach you everything about the stat command in Linux-based operating systems. We will look at the stat command, why it is used, the syntax of the stat command, and the options used along with it.

We will also look at some fantastic practical examples of the stat command by pairing it with various options to understand its working. So pay attention, take notes, and read to the end for the best benefits.

What is the stat command in linux?

The stat command is a command-line-based utility in the Linux-based operating systems that gives information about the file and filesystem. This information includes the file size, access permissions and user ID, group ID, and birth time access time of the file.

The stat command can also provide the file system information. It comes in handy when we want the information on any file. It sounds similar to the ls command (with the option “-l” for long format), but it is quite different.

The stat command also has options that specify a specific format instead of the default format for displaying information. We will take a look at these options in later sections. In a nutshell, the stat command displays detailed information about given files or file systems.

What is the syntax of linux stat command?

The syntax of the stat command is extremely simple. See for yourself.

stat <OPTIONS><FILE>

Let us take a closer look at the fields present in the syntax of the stat command.

1. <OPTIONS>

This particular field takes in a variety of options that specify how the comm command must function, format, and print the output. You can also specify more than one option in this field.

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

2. <FILE>

This file specifies the name of the file you want detailed information about. If the file you want to display information about is not in your current working directory, you can specify the file’s path.

Options used with linux stat command

Unlike most commands in Linux, the stat command comes with very few options. Let us take a detailed look at each.

1. -f

This option displays the filesystem status instead of the file status. You can also write this option as “–filesystem.”

2. -c

This option uses the format you specified instead of the default one. You can also write this option as “–format.”

3. -L

This option follows links. This option can also be written as “–dereference.”

4. -Z

This option prints the security context of SE Linux. This option can also be written as “–context.”

5. -t

This option prints the information in the terse form. You can also write this option as “–terse”.

6. –help

This option displays the help menu of the stat command.

help menu of the stat command

7. –version

This option displays the version of the stat command you are using.

version of the stat command

Format specifiers for files

As we saw, we can use the option “-c” to specify the format we want. Here is a list of all the format specifiers you can use on files:

1. %A – Access rights in human-readable form
2. %a – Access rights in octal form
3. %B – The size in bytes of each block is reported by “%b.”
4. %b – Number of blocks allocated
5. %C – Security context of SE Linux
6. %D – Device number in hex format
7. %d – Device number in decimal format
8. %F – File type
9. %f – Raw mode in hex
10. %G – Group name of the owner
11. %g – GID of owner
12. %h – Number of hard links
13. %i – Inode number
14. %n – File name
15. %o – IO block size
16. %s – Total size
17. %T – Minor device type in hex format
18. %t – Major device type in hex format
19. %U – Username of the owner
20. %u – UID of the owner
21. %X – Time of last access as seconds since Epoch
22. %x – Time of last access
23. %Y – Time of last modification as seconds since Epoch
24. %y – Time of last modification
25. %Z – Time of last change as seconds since Epoch
26. %z – Time of last change

Format specifiers for filesystems

Similar to format specifiers for files, we have format specifiers for filesystems that we can use with the option “-c” Here is a list of all the format specifiers you can use on filesystems:

1. %a – Free blocks available to non-superuser
2. %b – Total data blocks in the file system
3. %c – Total file nodes in the file system
4. %C – security context of SE Linux
5. %d – Free file nodes in the file system
6. %f – Free blocks in the file system
7. %i – File System id in hex
8. %l – Maximum length of file names
9. %n – File name
10. %T – Type in human-readable form

Understanding the output of linux stat command

Before we look at how to use the various options of the stat command, let us understand the fields in the output of the stat command.

understanding the output of the stat command

In the output shown above, there are various fields like file, size, change, birth, etc. Let us take a brief look at each field and what information it gives.

1. File

This particular field displays the name of the file. This name is the same as the name we passed to stat on the command line. However, the name can be different if we look at a symbolic link.

2. Size

This field displays the size of the file. By default, the size is represented in bytes.

3. Blocks

This field represents the number of filesystem blocks the file requires to be stored on the hard drive.

4. IO block

This field represents the size of a filesystem block.

5. File type

This field represents the type of object the metadata describes. It is usually a directory or file. However, we can sometimes deal with links, pipes, sockets, etc.

6. Device

This field represents the device number in hexadecimal and decimal formats. The device number is the ID of the hard drive on which the file is stored.

7. Inode

As the name suggests, this field represents the inode number. This number is the ID of this inode. A file is uniquely identified using the Inode number and the device number.

8. Links

This field represents the number of hard links pointed to this file. Every time a hard link is generated or deleted, this number will be adjusted up or down. Whenever it reaches zero, the file itself has been deleted, and the inode is removed.

9. Access

This field represents the file permissions. Permissions are shown in both octal and traditional “read, write and execute” forms.

10. UID

This field represents the user identification number and account name of the owner.

11. GID

This field represents the group identification number and account name of the owner.

12. Access

This field represented the access timestamp when the file was last accessed.

13. Modify

This field represented the modification timestamp when the file’s contents were last modified.

14. Change

This field represents the change timestamp. This was when the file’s attributes or contents were last changed.

15. Birth

This field shows the original creation date of the file.

Now, since we have covered the theory and fundamentals regarding the stat command, let us look at some fantastic examples of the comm command in the terminal of ubuntu 20.04.

Displaying the filesystem information

If you want to display information regarding the filesystem, run the stat command by pairing it with the option “-f” as shown:

stat -f <filename>

displaying the filesystem information

Let us understand the different fields in the output shown above.

1. File: This field displays the name of the file.

2. ID: This field displays the file system ID in hexadecimal format.

3. Namelen: This field represents the maximum length of a file name.

4. Fundamental block size: It is the total size of each block on the file system.

5. Blocks:

  • Total: It is the total number of blocks in the file system
  • Free: It is the total number of free blocks in the file system
  • Available: It is the total number of free blocks available for non-root users

6. Inodes:

  • Total: It is the total number of inodes in the file system.
  • Free: It is the total number of free inodes in the file system.

Using the stat command with multiple files

If you want to get the detailed information for multiple files, all you have to do is specify multiple filenames separated by a space as shown below:

stat <filename1> <filename2>

using the stat command with multiple files

Dereferencing Symlinks

If we provide the stat command with a symlink, it will give the information about the symbolic link instead of the original file. Here is an example:

dereferencing symlinks

 

However, if u want to follow or dereference the symbolic and display information about the file, execute the stat command by pairing it with the option “-L” as shown:

stat -L <filename>

Here is the same example shown, except this time, we dereference the symlink:

 

example of dereferencing symlinks

Customizing the output

The stat command allows us to customize the output instead of using the default format. There are two ways we can customize the output.

The first method uses the option “-C” or “format”. Providing multiple files to the stat command with the –format option in this method will automatically add a new line after each operand’s output. Use the syntax shown:

stat --format='<format specifier>' <filename>

customizing the output

In the above output, the format specifier “%n” simply prints the name of the file.

The second method to print a customized output is to use the “–print” option. Using this method, we can also provide trailing characters to the format. For example, if you want to add a new line after each line output, we can use the escape sequence “\n” following characters with a backslash. Use the following syntax:

adding a new line after each line output

Displaying information in the terse form

If you want to display information in the terse form, all you have to do is to execute the stat command by combining it with the option “-t,” as shown:

stat -t <filename>

displaying information in the terse form

Understanding timestamps

Before we conclude, let us understand the timestamps in the output of the stat command. Usually, the timestamps show the time the file was created from the UTC timezone.

Don’t get duped by the name. It is very simple to understand if the value is positive, then the file is created ahead of the UTC timezone. If at all the value is negative, the file is created before the UTC timezone.

For example, in the output shown below, the timestamp is “+0530”. This means the file is created 5 hours and 30 minutes ahead of UTC time.

understanding timestamps

Summary

As you have seen, the stat command is a simple yet powerful and useful tool that gives detailed information about the file and filesystem.

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *