Oct 24, 2023 5 min read

Stat Command in Linux

Use stat command in Linux with our step-by-step tutorial. The stat command is a command-line tool that shows detailed information about files.

Stat Command in Linux
Table of Contents

Before we talk about stat command in Linux, let's first understand-What is Stat Command ?

stat is a command-line tool that shows detailed information about files and file systems. It is used to display detailed information about files, directories, or file-systems. It provides various attributes such as file size, permissions, timestamps, inode information, and more. The stat command is helpful for examining file metadata, querying specific attributes, and scripting purposes.

In this tutorial, we have explained the stat command.  We will also address a few FAQs on stat Command in Linux.

Using the stat Command

The stat command has the following syntax:

stat [OPTION]... FILE...

stat accepts one or more FILE names as input and provides a number of options for controlling the command's behavior and output.

Take a look at the following scenario:

stat file.txt

You will get an output like below:

File: file.txt
  Size: 4030      	Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d	Inode: 13633379    Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/   linuxize)   Gid: ( 1000/   linuxize)
Access: 2019-11-06 09:52:17.991979701 +0100
Modify: 2019-11-06 09:52:17.971979713 +0100
Change: 2019-11-06 09:52:17.971979713 +0100
 Birth: -

Stat displays the following file information when run without any options:

  • Filename - The file's name.
  • Size - The file's size in bytes.
  • Blocks - The number of allotted blocks used by the file.
  • IO Block - Every block's size in bytes.
  • Type of file - (ex. regular file, directory, symbolic link.)
  • Device - Device number in hex and decimal.
  • Inode - Inode number.
  • Links - The number of hard links.
  • Access - File permissions in the numeric and symbolic methods.
  • Uid - User ID, as well as the owner's name.
  • Gid - Group ID and the owner's name.
  • Context - The SELinux security context.
  • Access - The most recent time the file was visited.
  • Modify - When the file's content was last modified.
  • Change - When the file's attribute or content was last changed.
  • Birth - Time it took to make the file (not supported in Linux).

Displaying Information About the File System

Instead of getting information about the file itself, use the -f, (--file-system) option to receive information about the file system where the provided file resides:

stat -f file.txt

You will get an output like below:

Output

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 run with the -f option, the following information is displayed:

  • File - The file's name.
  • ID - File system ID in hex.
  • Namelen - Maximum file name length.
  • Fundamental block size - The size of each block in the file.
  • Blocks

1) Total - Number of total blocks in the file system.
2) Free - The number of blocks in the file system that are available for use.
3) Available - Free blocks available for non-root users.

  • Inodes

1) Total - Number of inodes in the file system.
2) Free - The number of inodes in the file system that are free.

Stat does not follow symlinks by default. If you run the command on a symlink instead of the file it points to, the result will include information about the symlink:

stat /etc/resolv.conf
Output

File: /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf
  Size: 39        	Blocks: 0          IO Block: 4096   symbolic link
Device: 801h/2049d	Inode: 8126659     Links: 1
Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-11-06 21:12:26.875956073 +0100
Modify: 2018-07-24 11:11:48.128794519 +0200
Change: 2018-07-24 11:11:48.128794519 +0200
 Birth: -

Use the -L, (--dereference) option to dereference (follow) the symlink and display information about the file to which it points:

stat -L /etc/resolv.conf
File: /etc/resolv.conf
  Size: 715       	Blocks: 8          IO Block: 4096   regular file
Device: 17h/23d	Inode: 989         Links: 1
Access: (0644/-rw-r--r--)  Uid: (  101/systemd-resolve)   Gid: (  103/systemd-resolve)
Access: 2019-11-06 20:35:25.603689619 +0100
Modify: 2019-11-06 20:35:25.555689733 +0100
Change: 2019-11-06 20:35:25.555689733 +0100
 Birth: -

Customizing the Output

The stat command offers two options for customising the output to meet your needs: -c, (--format="format"), and --printf="format".

The difference between these two options is that --format adds a newline after each operand's output when two or more files are used as operants. Backslash escapes are interpreted by the --printf command.

With --format and --printf, you can utilise a variety of format directives for files and file systems.

For example, if you only wanted to see the file type, you could type:

stat --format="%F" /dev/null
Output

character special file

You can combine as many formatting directives as you want and use custom separators between them if you want. A single character or a string can be used as a separator:

stat --format="%n,%F" /dev/null
Output

/dev/null,character special file

Use the --printf option to understand special characters like newline or tab:

stat --printf='Name: %n\nPermissions: %a\n' /etc

\n prints a new line:

Output

Name: /etc
Permissions: 755

The information can also be displayed in a terse format by the stat. This format is useful for other programmes to parse.

To print the output in terse style, run the command with the -t (--terse) option:

stat -t /etc
Output

/etc 12288 24 41ed 0 0 801 8126465 147 0 0 1573068933 1573068927 1573068927 0 4096

In your terminal, type man stat or stat --help to get a complete list of all format directives for files and file systems.

FAQs on Stat Command in Linux

How do I use the stat command?

To use the stat command, simply type stat filename, replacing filename with the name of the file or directory you want to examine.

What information does the stat command provide?

The stat command provides various attributes, including file size, permissions, timestamp of last access, timestamp of last modification, file type, inode number, and more.

Can I use the stat command on multiple files at once?

Yes, you can specify multiple filenames as arguments to the stat command, and it will display information for each file individually.

How can I output the stat result in a specific format?

The stat command allows you to output the result in a customized format using the --format option followed by a format specifier. For example, stat --format="%s %n" filename shows only the file size and filename.

Can I recursively use the stat command on directories and subdirectories?

Yes, you can use the stat command recursively on directories and subdirectories by combining it with the find command. For example, find . -type f -exec stat {} \; will display stat output for all files in the current directory and its subdirectories.

How can I check the size of a file using the stat command?

To check the size of a file, use the stat --format=%s filename command. It will display the size of the file in bytes.

Can the stat command display the file permissions in human-readable format?

Yes, you can display file permissions in human-readable format using the stat -c "%A" filename command. It will show the permissions as rwxrwxrwx.

Conclusion

The stat command displays information about the files and file systems that are specified.

Several additional commands in Linux can display information about supplied files, with ls being the most commonly used, however it only provides a portion of the data provided by the stat command.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.