In one of the previous articles, we talked about the fact that each file and folder in the file system has a so-called Inode structure that stores the metadata of this object. It stores the owner, the group of the owner, the time of modification, creation and access to the file, as well as other information. It can be seen not only with the help of file system debugging tools.

Some of this information is shown by the ls utility, but if you need more, you can use the stat command. In this article, we will look at how to use this command in Linux.

stat command in linux

The command syntax is very simple. It needs to pass options and the path to the file for which you want to see the information:

$ stat options /path/to/file

It is not necessary to pass options and there are not many of them:

  • -L, dereference – show file information instead of symbolic link;
  • -f, –file-system – show information about the file system in which the file is located;
  • -c, –format – allows you to specify the output format instead of the standard one, each file is output from a new line;
  • –printf – similar to –format , but you need to use n for newline;
  • -t, –terse – display information in a very short form, in one line;
  • –version – show utility version.

These are all command options. Now let’s look at some usage examples. To view information about a file, just run the program without options, passing it the path to the file, for example /etc/passwd:

stat /etc/passwd

Linux stat Command

Consider what the output of the program means:

  • File (File) – the path to the file on which information is displayed;
  • Size (Size) – file size in bytes;
  • Block I/O (IO Block) – block size of the file system in bytes;
  • Blocks – the number of file system blocks occupied by the file;
  • Device (Device) – identifier of the device, for example HDD, on which the file is saved;
  • Inode – unique Inode number of this file;
  • Links – number of hard links to this file;
  • Access (Access) – file access rights;
  • Uid – identifier and name of the user-owner of the file;
  • Gid – identifier and name of the file group;
  • Access (Access) – time of the last access to the file;
  • Modified (Modify) – the time when the content of the file was last changed;
  • Changed – the time when the file attributes or file content was last changed;
  • Created (Birth) – Reserved to display the original creation date of the file, but not yet implemented.

We need to talk a little more about the time format. For example, the last time the file was accessed is 2020-12-02 18:25:01.043831739 +0200. This time is shown taking into account the time zone. And the numbers +0200 show that the time zone on the computer that created or modified this file is two hours longer than UTC, that is, Europe/Kiev in winter.

If you try to pass a symbolic link to the utility, it will show information only from the Inode of the link itself:

stat /etc/passwdlink

Linux stat Command

In order to see information about the file pointed to by the link, you must use the -L option:

stat -L /etc/passwdlink

Linux stat Command

The utility can transfer not one file, but several:

stat /etc/passwd /etc/group

Linux stat Command

And here you already need the ability to customize the output format. You can use the following character sequences to format the output:

  • %A – access rights;
  • %b – the number of occupied blocks;
  • %F – file type;
  • %g – file group identifier;
  • %G – file group name;
  • %i – Inode identifier;
  • %n – filename;
  • %s – file size;
  • %u – file owner identifier;
  • %U – file owner name;
  • %x – last access time;
  • %y – time of the last content modification;
  • %z is the time when the content or attributes were last modified.

These are not all possible sequences, you can find more in the utility help:

man stat

For example, let’s print just the name of a file, and the last modification time of its contents:

stat --printf "File %n has been modified %yn" /etc/passwd /etc/group

Linux stat Command

If you want to see information about the file system in which a file is located, then you need to use the -f option :

stat -f /etc/passwd

Linux stat Command

Let’s look at what the fields that the utility displays mean:

  • File (File) – file name;
  • Type (Type) – file system type;
  • ID – file system identifier;
  • Name length (Namelen) – the maximum length of a name in the file system;
  • Block size – the amount of data for a read or write request for optimal performance;
  • Fundamental block size – the physical block size in the file system.

Next come the total number of blocks in the system and the number of free blocks.

Findings

In this short article, you learned what the Linux stat command is. As you can see, this is a very useful command that allows you to view low-level information about files and the file system.

Source: https://losst.ru/. The article is distributed under the CC-BY-SA license