Disk Management Utilities

How to Use the Linux stat command

The stat command is a useful utility that reports info about a certain file or filesystem. It comes pre-installed with almost all the Linux systems out there, so no need to worry about installation.

Let’s check out how to use stat to get all the important data you need for a specific file/filesystem.

Why stat command

Sometimes, you may want to know some key details about a certain file/filesystem, for example, the file’s size, access permissions, inode number, time of last access/modification etc. Agreed, you can check out a lot of details about a file using ls. However, stat offers far more in-depth about the target file/filesystem.

When should you use stat? Whenever you need those extra information. For a quick comparison, let’s check out the file info of file1.txt. To better understand the output, learn more about the lh command.

$ ls -lh file1.txt

Now, let’s check out what stat has to offer.

$ stat file1.txt

That’s a lot of information! You don’t need all of them in everyday life but in some specific situation, stat comes in super handy.

Linux Stat Usage: Location

Most of the Linux commands run from the /usr/bin directory.

$ which stat

Linux Stat Usage: Checking file/filesystem info

We’ve already seen this in action, right? The command structure goes like this.

$ stat <option> <file_filesystem>

To see the in-depth info of a file/filesystem, run this command. In this case, it’s my trusty file1.txt.

$ stat file1.txt

There are TONS of information about the specific file, right? All the information presented is labelled. Depending on your requirement, pick up the important one. From my experience, the most used ones are file permissions, inode and Uid and/or Gid.

Now, let’s check out an example with a filesystem. In this case, it’ll be the mount point of the filesystem. For example, this command will show the information of the root filesystem.

$ stat /

In Linux, (almost) every single thing is a file. Any filesystem is a file itself, so the output won’t be any different.

Linux Stat Usage: Terse form

When you’re running stat normally, all the information is printed in a human-readable structure. Want a short and simple version of the output? Add the “-t” argument.

$ stat -t /

The terse format is a special format that uses a predefined list of stat format specifiers.

Linux Stat Usage: Custom format

This is another interesting usage of the stat command where you can design the desired output structure of stat. To do this task, stat offers a LONG list of available format specifiers.

Here’s how the command looks like.

$ stat --format=<format_specifiers> <file>

For example, the “%A” specifier returns the file/filesystem permissions in a human-readable format.

$ stat --format=%A file1.txt

The “%U” specifier returns the owner of the file/filesystem.

$ stat --format=%U /

To get the size of the file, use the “%s” format specifier.

$ stat --format=%s file1.txt

Want the file type? Use “%F”.

$ stat --format=%F file1.txt

For the inode number, use “%i”.

$ stat --format=%i file1.txt

Now, let’s combine all of them together into a single command line. It will look like this.

$ stat --format="%A %U %s" /

There are tons of other format specifiers that stat support. All of them are listed in the man page of stats.

Now, as we’ve previously seen the “-t” or “–terse” argument, it’s a predefined value for the following arguments.

$ stat --format="%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %W %o %C"
 <file_filesystem>

As for the “–terse –file-system” argument, the predefined value is as follows.

$ stat --format="%n %i %l %t %s %S %b %f %a %c %d" <file_filesystem>

These outputs are very useful if you’re using stat in any sort of script, especially in bash scripts. New to bash script? Check out the beginner’s guide to bash scripting.

Printf vs format

The stat command supports –printf argument that functions basically the same manner as –format. However, the main difference between them is how the output is produced.

Let’s get it clear with an example. Here, I’m running stat with both arguments with the same format specifier.

$ stat --format="%A %U %s" file1.txt
$ stat --printf="%A %U %s" file1.txt

As we can see, the –format argument adds a new line after the output ends. However, the –printf doesn’t. To make sure that there’s a new line after the output, you have to add “\n” at the end of the format specifier string.

$ stat --printf="%A %U %s\n" file1.txt

Differentiate between file and link

In certain situations, you might be actually working with a link. However, by default, stat doesn’t differentiate between a link and a physical file. There’s a dedicated argument to get around this issue. Just pass the “-L” argument.

$ stat -L <file_filesystem>

Final thoughts

The stat tool is a pretty straightforward one. All of its functions are managed by simple arguments and flags. Once you know which one to use, you can reap the most benefit out of it.

To learn further in-depth about the stat command, I highly recommend checking out the man page. It contains all the supported format specifiers and some additional arguments that you may find interesting.

$ man stat

Enjoy!

About the author

Sidratul Muntaha

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.