[email protected]Support 24/7 · Billing 09:00 – 00:00LinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedMarch 13, 2023

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.

Keep reading
How To Create A Password Generator In Bash.
July 19, 2023

How To Create A Password Generator In Bash.

How To Create A Password Generator In Bash. In this article, we will explore how to create a password generator using Bash scripting. Bash is a popular scripting language available in most Unix-like operating systems. Generate a random string of characters: To create a secure password, we need to…

Read article
How to understand and use BASH conditionals.
July 18, 2023

How to understand and use BASH conditionals.

How To Understand And Use BASH Conditionals. BASH (Bourne Again SHell) is a popular command-line interpreter and scripting language used in Unix-like operating systems. One of the key features of BASH is its support for conditionals and harnesses, which allow you to make decisions and control the…

Read article
How to understand Environment Variables in Bash
July 18, 2023

How to understand Environment Variables in Bash

How To Understand Environment Variables In BASH Environment variables play a crucial role in the BASH shell, allowing you to customize and configure your shell environment. In this article, we’ll explore various examples of how to work with environment variables in BASH. Viewing Environment…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, cloud hosting, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Support 24/7 · Billing 09:00 – 00:00