Stat command, some basic examples in Ubuntu

about stat command

In the next article we will take a look at some basic examples of the stat command. This command for Gnu / Linux is a command line tool used to display detailed information about a file or file system. The stat command is part of the GNU Core Utilities, which are tools that come pre-installed on practically all UNIX and Gnu / Linux systems.

Some users think of the stat command as an improved version of the ls -l command. While the -l flag provides more details about files, such as file ownership and permissions, the stat command digs deeper and provides more information.

Command stat

The stat command syntax Gnu / Linux is the following:

stat [OPCIONES] NOMBRE DEL ARCHIVO

Stat command with no arguments

If we don’t use any option, the stat command shows the default output. If we wanted to see the details of a file located in the current directory, we would only have to execute in a terminal (Ctrl + Alt + T):

stat command with no arguments

stat archivo1.txt

When invoked without any options, stat displays the following information from the file:

  • File: the Name from the file.
  • Size: the file size in bytes.
  • Blocks: the number of allocated blocks which takes the file.
  • I / O block: the size in bytes of each block.
  • Type of file: regular file, directory, symbolic link …
  • Device: device number in hexadecimal and decimal.
  • Inode: inode number.
  • Links: number of physical links.
  • Access: file permissions in the numeric and symbolic methods.
  • Uid: User ID and owner name.
  • Gid: Group ID and owner name.
  • Access: the last time the file was accessed.
  • Modification: the last time content was modified from the file.
  • Change: the last time the attribute or content was modified from the file.

View information about multiple files

We can also view a detailed report on multiple files:

multi-file stat command

stat archivo1.txt archivo2.pdf

Show file system status

This tool can check the status of the filesystem where the file is located using the -f option. This will show the block size, total and available memory, to mention just a few attributes:

stat folder

stat -f /home

Show information in basic format

The -t option is used to display information in a basic format:

basic format stat command

stat -t archivo1.txt

Enable symbolic link tracking

As usual, if we run the stat command against a symbolic link, it will only provide information about the link and not about the file that the link points to:

stat command symbolic link

To get information about the file that the links point to, we will have to use the -L option, also known as the unreference option:

command stat symbolic link file

stat -L archivo1.txt

This will show information about the file and not about the link.

Format sequence

So far we have seen that the stat command prints a lot of information to the terminal. If you are interested in specific information, the output can be customized using a format sequence, which will give us exactly what we need, omitting the other details. The most popular expressions used to customize the output include the option –Format or –Printf.

For show access rights and uid (user ID) we will use the format sequences %to Y %or.

show access rights

stat --printf="%a:%un" archivo1.txt

In case of wanting view inode and access rights, we can also use the option –Format:

see only inode and file access rights

stat --format="%i:%a" archivo1.txt

Format sequences

Some of the sequences of formats that we can use are:

format sequences to A

% a → It will show the access rights in octal format.
% A → Shows the access rights in a human-readable format.

format sequence b B

% b → Prints the number of allocated blocks.
% B → The size in bytes of each block reported by% b.

format sequence d D

% d → Shows the device number in decimal format.
% D → The device number in hexadecimal format.

g format sequence G

% g → Prints the Owner’s group ID.
% G → Shows the owner’s group name.

format sequence n N

% n → Shows the file name.
% N → Write the filename in quotes without reference if it is a symbolic link.

u U format sequences

% u → Shows the Owner User ID.
% U → Prints the owner username.

w w format sequences

% w → Reveal the file birth time, human readable. Write – if unknown.
% W → Prints the file birth time, in seconds from Epoch. Write 0 if unknown.

x X format sequences

% x → Will print the time of last access, human readable.
% X → The time of last access, in seconds from Epoch.

sequences and AND

% y → Shows the time of last modification, human readable.
% Y → Prints the time since last modification, in seconds from Epoch.

z Z format sequences

% z → This is the time of last change, human readable.
% Z → The hour since last change, in seconds from Epoch.

Get help

For get more command options, you just have to execute the following command:

stat help

stat --help

You can also refer to the man pages.

Add Comment