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.
Now, let’s check out what stat has to offer.
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.
Linux Stat Usage: Checking file/filesystem info
We’ve already seen this in action, right? The command structure goes like this.
To see the in-depth info of a file/filesystem, run this command. In this case, it’s my trusty 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.
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.
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.
For example, the “%A” specifier returns the file/filesystem permissions in a human-readable format.
The “%U” specifier returns the owner of the file/filesystem.
To get the size of the file, use the “%s” format specifier.
Want the file type? Use “%F”.
For the inode number, use “%i”.
Now, let’s combine all of them together into a single command line. It will look like this.
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.
As for the “–terse –file-system” argument, the predefined value is as follows.
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 --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.
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.
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.