Methods of payment Abuse

What the stat command does in Linux

14.11.2021, 16:15

Each file and folder in the file system has a so-called Inode structure where the metadata of this object is stored. It stores the owner, owner group, 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. Certain information is shown by the ls utility, but if you need more, you can use the stat command. Next we will tell you how to use this command in Linux.

What the command does

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

$ stat options /path/to/file.

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

 -L, dereference - show file information instead of a 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, only for a new line you should use ;

-t, --terse - shows information in a very brief form, in one line;

--version - show the version of the utility.

Let's see some examples of usage. To see the information about a file, it is enough to run the program without options by passing it the path to the file, for example /etc/passwd:

$ stat /etc/passwd

Program output:

File - the path to the file for which the information is shown;

Size - the size of the file in bytes;

IO Block - the size of the file system block in bytes;

Blocks - the number of file system blocks occupied by the file;

Device - identifier of the device, for example HDD, on which the file is saved;

Inode - unique Inode number of the file;

Links - the number of hard links to this file;

Access - access rights to the file;

Uid - identifier and name of the user-owner of the file;

Gid - identifier and name of the file group;

Access - the time of the last access to the file;

Modify - the time when the content of the file was last modified;

Changed - the time when the file attributes or file content was last changed;

Created (Birth) - reserved for displaying the original creation date of the file, but not yet implemented.

We need to talk a bit more about the time format. For example, the time of last access to the file 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 more than UTC, i.e. Europe/Kiev in winter time.

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

$ stat /etc/passwdlin

To see information about the file pointed to by the link you should use the -L option:

$ stat -L /etc/passwdlink

You can pass not just one file, but several:

$ stat /etc/passwd /etc/group

To format the output, you can use the following character sequences:

 %A - access rights;

%b - number of occupied blocks;

%F - file type;

%g - file group identifier;

%G - file group name;

%i - Inode identifier;

%n - file name;

%s - file size;

%u - file owner identifier;

%U - file owner name;

%x - time of last access;

%y - time of last content modification;

%z - time of last modification of content or attributes.

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

$ man stat

For example, let's output only the name of a file and the time of the last modification of its content:

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

If you want to see information about the file system where the file is located, you should use the -f option:

$ stat -f /etc/passwd

Let's take a look at what the fields displayed by the utility mean: 

File - the name of the file;

Type - file system type;

ID - file system identifier;

Name length (Namelen) - the maximum length of the name in the file system;

Block size - the amount of data in a read or write request for optimal speed;

Fundamental block size - physical block size in the file system.

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