How to Use the stat Command on Linux

How to Use the stat Command on Linux

Here we can see, “How to Use the stat Command on Linux”

The stat command in Linux provides a lot more information than ls. So, with this informative and flexible utility, you may take a look behind the curtain. But, first, we’ll demonstrate how to use it.

stat Takes You Behind the Scenes

The ls command is excellent at what it does—and it does many things—but with Linux, there always appears to be a way to delve deeper and see what’s beneath the surface. And it’s not always as simple as lifting the carpet’s edge. You can start by ripping up the floors and digging a hole. After that, Linux can be peeled like an onion.

ls will display a lot of information about a file, including its permissions, how big it is, and if it is a file or a symbolic link. To show this data, ls read it from an inode, a file system structure.

An inode can be found in every file and directory. The file’s metadata, such as the filesystem blocks it occupies and the date stamps associated with it, are stored in the inode. Thus, the inode acts as a file’s library card. However, ls will only display a portion of the data. We’ll need to run the stat command to see everything.

Also See:  how to make google chrome transparent windows 10

The stat command, like ls, provides several options. As a result, it’s an excellent candidate for aliasing. Wrap a set of options that makes stat gives you the output you want in an alias or shell function once you’ve found them. This makes it much easier to use and eliminates memorizing a long list of command-line arguments.

A Quick Analysis

Let’s use ls to get a long listing (-l option) and file sizes that are human-readable (-h option):

ls -lh ana.h

The following is a list of the information provided by ls, from left to right:

  • The initial character in the file is a hyphen “-,” which indicates that it is a conventional file rather than a socket, symlink, or another form of an object.
  • In octal format, the owner, group, and other permissions are listed.
  • The total number of hard links to this file. It will be one in this scenario, as it is in the vast majority of circumstances.
  • Dave is the owner of the file.
  • Dave is the group’s owner.
  • The file is 802 bytes in size.
  • On Friday, December 13, 2015, the file was last edited.
  • Ana.c is the name of the file.

Let’s have a look at some statistics:

stat ana.h

Stat provides us with the following data:

  • Filename: The file’s name. It’s usually the same as the name we gave stat on the command line, but it can be different if we’re looking at a symbolic link.
  • Size: The file’s size in bytes.
  • Blocks: The number of filesystem blocks required to store the file on the hard drive.
  • IO Block: A filesystem block’s size is measured in blocks.
  • File type: The sort of object that the metadata specifies is referred to as the file type. Files and directories are the most frequent, but they can also be links, sockets, or named pipes.
  • Device: The hexadecimal and decimal number of the device. The ID of the hard drive on which the file is saved.
  • Inode: The number of the inode. That is this inode’s ID number. The inode number and the device number work together to identify a file.
  • Links: This number represents the number of hard links that point to this file. Each inode on a hard link is unique. Another way to look at this number is to consider how many inodes point to this one file. This number will be modified up or down each time a hard link is created or destroyed. This is because the file has been erased, and the inode has been removed when it approaches zero. This number represents the number of files in a directory, including the “.” entry for the current directory and the “..” entry for the parent directory, if the stat is used on it.
  • Access: The file permissions are displayed in octal and standard rwx formats (read, write, execute formats).
  • Uid: The owner’s user ID and account name.
  • Gid: The owner’s group ID and account name.
  • Access: The timestamp for access. It’s not as simple as it may appear. relatime is a mechanism used by modern Linux distributions to reduce the number of hard drive writes required to update access. This is because if the access time is older than the modified time, it is updated.
  • Modify: The timestamp of the modification. This is the last time the contents of the file were changed. (As luck would have it, this file’s contents were last modified four years ago today.)
  • Change: The timestamp for the change. This is the most recent update to the file’s attributes or contents. The change timestamp will be updated (since the file characteristics have changed). Still, the modified timestamp will not be updated if you edit a file by establishing new file permissions (because the file contents were not changed).
  • Birth: This field is reserved for displaying the file’s original creation date, but it isn’t used in Linux.

Understanding the Timestamps

The timestamps are affected by the timezone. For example, the -0500 at the end of each line indicates that the file was generated on a computer in a Coordinated Universal Time (UTC) timezone five hours ahead of the present computer’s timezone. As a result, this machine is five hours behind the one that created the file. In actuality, the file was created on a computer in the United Kingdom time zone, and we’re viewing it on a computer in the United States Eastern Standard time zone.

Because their names sound similar to the uninitiated, the modify and change timestamps might confuse.

Also See:  Netflix adds spatial audio support for iOS app, but you’ll need AirPods

Let’s change the permissions on a file called ana.c with chmod. We’re going to make it so that anyone can write it. This will not change the file’s content, but it will change the file’s characteristics.

chmod +w ana.c

Then we’ll look at the timestamps with stat:

stat ana.c

The modification timestamp has not been updated, but the change timestamp has.

Only if the file’s contents change will the modified timestamp be updated. For both content and attribute modifications, the change timestamp is updated.

Using Stat With Multiple Files

Pass the filenames to stat on the command line to have stat report on many files at once:

stat ana.h ana.o

Use pattern matching to apply stat on a group of files. The question mark represents any single character “?” and any string of characters is represented by the asterisk “*.” For example, with this command, we may instruct stat to report on any file named “ana” with a single letter extension:

stat ana.?

Using stat to Report on Filesystems

stat can give you information about the health of filesystems and files. The -f (filesystem) option instructs stat to report on the filesystem that contains the file. Stat can also be passed a directory, such as “/,” rather as a filename.

stat -f ana.c

The following is the data that stat provides:

  • Filename: The file’s name.
  • ID: The hexadecimal representation of the filesystem ID.
  • Namelen: The maximum length for file names that can be used.
  • Type: The filesystem’s type.
  • Block size: The amount of data requested for reading requests to get the best data transfer rates.
  • Fundamental block size: The size of each filesystem block in terms of its actual block size.


  • Total: This is the total number of blocks in the filesystem.
  • Free: The number of unallocated blocks in the filesystem.
  • Available: Regular (non-root) users have access to a limited number of free blocks.


  • Total: The filesystem’s total number of inodes.
  • Free: The number of unallocated inodes in the filesystem.

Dereferencing Symbolic Links

Stat will report on a file that is truly a symbolic link if you use it on it. Use the -L (dereference) option if you want stat to report on the file that the link points to. Code.c is a symbolic link to the file ana.c. Let’s take a look at it without the -L parameter:

stat code.c

The filename indicates that code.c refers to ( -> ) ana.c. The file is merely 11 bytes long. This connection is stored in a block with zero blocks. A symbolic link is specified as the file type.

We’re not looking at the original file. Let’s try it again, this time using the -L option:

stat -L code.c

The file information for the file pointed to by the symbolic link is now displayed. It’s worth noting, though, that the filename is still specified in the code. d. This is the link’s name, not the target files. This is because we used this name while calling stat from the command line.

The Terse Report

Stat produces a shortened summary when the -t (terse) option is used:

stat -t ana.c

There are no hints provided. You’ll need to cross-reference this output to a full stat output to make sense of it until you’ve learned the field sequence.

Custom Output Formats

Using a custom format to get a different set of data from stat is a better option. Format sequences are a large collection of tokens. Each one of these is a data element. Create a format string for the ones you wish to be included in the output. Then, when we use stat with the format string, the result will only contain the data pieces we asked for.

Files and filesystems each have their own set of format sequences. For example, the following is a file list:

  • %a: The access rights in octal.
  • %A: The access rights in human-readable form (rwx).
  • %b: The number of blocks allocated.
  • %B: The size in bytes of each block.
  • %d: The device number in decimal.
  • %D: The device number in hex.
  • %f: The raw mode in hex.
  • %F  The file type.
  • %g: The group ID of the owner.
  • %G: The group name of the owner.
  • %h: The number of hard links.
  • %i: The inode number.
  • %m: The mount point.
  • %n: The file name.
  • %N: The quoted file name, with dereferenced filename if it is a symbolic link.
  • %o: The optimal I/O transfer size hint.
  • %s: The total size, in bytes.
  • %t: The major device type in hex, for character/block device special files.
  • %T: The minor device type in hex, for character/block device special files.
  • %u: The user ID of the owner.
  • %U: The user name of the owner.
  • %w: The time of file birth, human-readable, or a hyphen “-” if unknown.
  • %W:  The time of file birth, seconds since the Epoch; 0 if unknown.
  • %x: The time of last access, human-readable.
  • %X: The time of last access, seconds since the Epoch.
  • %y: The time of last data modification, human-readable.
  • %Y: The time of last data modification, seconds since the Epoch.
  • %z: The time of last status change, human-readable.
  • %Z: The time of last status change, seconds since the Epoch.

The “epoch” is the Unix Epoch, which began at 00:00:00 +0000 on January 1, 1970. (UTC).

The format sequences for filesystems are as follows:

  • %a: The number of free blocks available to regular (non-root) users.
  • %b: The total data blocks in the filesystem.
  • %c: The total inodes in the filesystem.
  • %d: The number of free inodes in the filesystem.
  • %f: The number of free blocks in the filesystem.
  • %i: The file system ID in hexadecimal.
  • %l: The maximum length of filenames.
  • %n: The filename.
  • %s: The block size (the optimum writing size).
  • %S: The size of filesystem blocks (for block counts).
  • %t: The file system type in hexadecimal.
  • %T: file system type in human-readable form.

There are two options for accepting format sequence strings. —format and —printf are the two options. The distinction is that —printf interprets C-style escape sequences like newline n and tab t and does not automatically append a newline character to its output.

Let’s make a format string that we can provide to stat. We intended to utilize the format sequences percent n for filename, percent s for file size, and percent F for a file type. To ensure that each file is treated on a new line, we’ll append the n escape sequence to the end of the string. This is how our format string looks:

"File %n is %s bytes, and is a %F\n"

We’ll use the —printf option to feed this to stat. Stat will be asked to report on a file named code.c as well as a group of files that match ana.? The entire command may be found here. Between —printf and the format string, note the equals sign “=”:

stat --printf="File %n is %s bytes, and is a %F\n" code.c ana/ana.?

Each file’s report is listed on a separate line, as per our desire. We are given the filename, file size, and file type.

You can access even more data components with custom formats than you can with standard stat output.

Fine Grain Control

As you can see, there is a lot of room to extract the specific data pieces you’re interested in. You can probably see why aliases were chosen for the lengthier and more intricate incantations.


I hope you found this information helpful. Please fill out the form below if you have any queries or comments.

User Questions:

  1. What is the purpose of statin Unix?

The stat command on Unix-like operating systems displays the detailed status of a file or a file system.

  1. Can you explain what it means to stat a file?

Using the verb “stat” in the context of using and thinking about Unix system calls is very natural. To “stat” a file means to obtain some or all of its metadata.

  1. What exactly is a stat system?

In Linux, the stat system call is used to examine the status of a file, such as when it was last accessed. File attributes are returned by the stat() system function. For example, the Stat() method returns the file characteristics of an inode.

Also See:  Microsoft Edge is finally killing off one of the worst things about being online
  1. Why find + stat is much slower than ls –

Why find + stat is much slower than ls – from commandline

  1. Blocks in ‘Stat’ Command

Blocks in ‘Stat’ Command from linuxquestions