In one of the previous articles, we talked about the fact that every file and folder in the file system has a so-called Inode structure, which stores the metadata of this object. It stores the owner, the owner’s group, 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 the 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 on Linux.
The stat command in Linux
The command syntax is very simple. She needs to pass options and the path to the file for which you need to view information:
$ stat options /the path to the file
It is not necessary to transfer options and there are not many of them:
- -L, dereference – show information about a file 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, each file is displayed on a new line;
- –printf – similarly –format, only for a new line you need to use n;
- -t, –terse – displaying information in a very short form, in one line;
- –version – show the version of the utility.
These are all command options. Now let’s take a look at the 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:
Let’s see what the output of the program means:
- File – path to the file by which information is displayed;
- Size – file size in bytes;
- IO Block – file system block size in bytes;
- Blocks – the number of file system blocks occupied by the file;
- Device – device identifier, for example, HDD, on which the file is saved;
- Inode – the unique Inode number of this file;
- Links – the number of hard links to this file;
- Access – file access rights;
- Uid – identifier and name of the user who owns the file;
- Guide – file group identifier and name;
- Access – time of the last access to the file;
- Modified – time when the content of the file was last changed;
- Changed – time when file attributes or file content were last changed;
- Created by (Birth) – reserved for displaying 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 time of the 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 longer than UTC, that is, Europe / Kiev in winter time.
If you try to pass a symbolic link to the utility, it will show information only from the Inode of the link itself:
In order to see information about the file to which the link points, use the -L option:
stat -L /etc/passwdlink
The utility can transfer more than one file, but several:
stat /etc/passwd /etc/group
And here you need the ability to customize the output format. The following character sequences can be used 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 – file name;
- %s – file size;
- % u – file owner identifier;
- % U – the name of the owner of the file;
- %x – last access time;
- %and – time of the last modification of the content;
- %with – the time the content or attributes were last modified.
These are not all possible sequences, you can find more in the utility help:
For example, let’s display only the name of the file, and the time of the last modification of its contents:
stat --printf "File %n has been modified %yn" /etc/passwd /etc/group
If you want to view information about the file system in which the file is located, then you must use the option -f:
stat -f /etc/passwd
Let’s take a look at what the fields displayed by the utility mean:
- File – file name;
- 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 when requested to read or write for optimal speed of work;
- Fundamental block size – physical block size in the file system.
Then there are the total number of blocks in the system and the number of free blocks.
In this short article, you learned what the Linux stat command is. As you can see, this is a very useful command to view low-level information about files and the file system.