How to use stat command in linux.

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 [OPTION]... FILE...

 

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:

stat file.txt

The output will look something like this:

   File: ‘file.txt’
  Size: 15              Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 21273       Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: system_u:object_r:admin_home_t:s0
Access: 2023-01-28 18:13:02.958485049 +0200
Modify: 2023-02-16 17:26:56.339360585 +0200
Change: 2023-02-16 17:26:56.339360585 +0200
 Birth: -

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:

stat -f file.txt

The output of the command will look like this:

  File: "package.json"
    ID: 8eb53097b4494d20 Namelen: 255     Type: ext2/ext3
Block size: 4096       Fundamental block size: 4096
Blocks: Total: 61271111   Free: 25395668   Available: 22265851
Inodes: Total: 15630336   Free: 13979610

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.

  • Blocks:

    • 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.

  • Inodes:

    • Total – Number of total inodes in the file system.

    • Free – Number of free inodes in the file system.

 

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:

stat /etc/resolv.conf
    File: ‘/etc/resolv.conf’
  Size: 38              Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 3741        Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: system_u:object_r:net_conf_t:s0
Access: 2023-03-12 21:12:47.606928199 +0200
Modify: 2023-01-25 20:40:21.389200174 +0200
Change: 2023-01-25 20:40:21.389200174 +0200
 Birth: -

To dereference (follow) the symlink and display information about the file to which the symlink points, use the -L, (–dereference) option:

stat -L /etc/resolv.conf
  File: ‘/etc/resolv.conf’
  Size: 38              Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 3741        Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: system_u:object_r:net_conf_t:s0
Access: 2023-03-12 21:12:47.606928199 +0200
Modify: 2023-01-25 20:40:21.389200174 +0200
Change: 2023-01-25 20:40:21.389200174 +0200
 Birth: -

 

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:

stat --format="%F" /dev/null
character special file

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:

stat --format="%n,%F" /dev/null
/dev/null,character special file

To interpret special characters like newline or tab, use the –printf option:

stat --printf='Name: %n\nPermissions: %a\n' /etc

\n prints a new line:

Name: /etc
Permissions: 755

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:

stat -t /etc
/etc 4096 8 41ed 0 0 fd01 16386 99 0 0 1678125660 1678472051 1678472051 0 4096 system_u:object_r:etc_t:s0

For a complete list of all format directives for files and file systems type, man stat or stat –help in your terminal.

 

Conclusion

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.