The stat command gives detailed information about your files and directories it includes information about file size, permissions, timestamps, and a lot more.
In this tutorial, I will walk you through different examples of how you can use the stat command in Ubuntu.
So, let's start with the basics (I mean the syntax).
How to use the stat command in Ubuntu
To use any command, you have to learn its syntax first. Otherwise, it will give you multiple errors just based on the system and no one wants a headache for syntax. Right?
So here's the simple syntax of the stat command:
stat [options] filename
options, you can get tune the default output as per your needs
filenameis where you will append the filename of which you want the data
And if you execute the stat command without any options, it will get you the following output:
But you may be wondering, what is the meaning of each data field?
Let me break it down for you.
File: Indicates filename which may differ if your target was a symbolic link as it will follow the original file by default.
Size: Filesize in bytes.
Blocks: The total number of blocks used to store that file on the disk drive.
IO Block: Size of each block in bytes.
File type: Type of the target file. For me, it is a regular file but it can be special files, directories, or symbolic links.
Device: Shows device number in hexadecimal form.
Inode: Indicates the inode number of the file.
Links: Number of hard links associated with the file.
Access: Shows file permissions.
Uid: The user ID of the file owner.
Gid: The group ID of the file owner.
Access: Timestamp when the file was accessed last time.
Modify: When the file was modified last time.
Change: When the metadata of the file was changed.
Birth: Timestamp when the file was created/downloaded.
And as you can see, it is a lot of information and you may not want to have it every time you use the stat command.
There's where the
options comes to play letting you decide what additional information you want or what specific information you want to have.
1. Follow the symbolic link using the stat command
Personally, I use the stat command to follow symbolic links.
And to do that, all you have to do is append the symbolic filename to the stat command as shown here:
But what if you want to remove the additional information? You use the
-c%N option with the stat command:
stat -c%N Symbolic_link
2. Check the status of the filesystem
If you want to check the status of the filesystem, the stat command will do that for you which can easily be done using the
stat -f /path/to/filesystem
And as you can see, it gave me information including free space, filesystem type, ID, and more.
3. Use multiple files with the stat command
To use multiple files with the stat command, all you have to do is append their filenames separated with space as shown:
stat File1 File2
4. Print output in the TERSE format
TERSE is a lossless IBM archive format and the good news is you can use the stat command to print output in the TERSE format.
For that, you have to use the
-t flag as shown:
stat -t Filename
5. Get specific information
As you saw, the stat command prints tonnes of information and most of which is not important for casual users.
But the good thing is you can customize what you exactly want.
For that, you'd have to use the
--printf flag as shown here:
stat --printf='format_sequence:\n ' Filename
Here, you will have to replace the format_sequence with the respective sequence format.
And if you're curious why I used the
\n, then there's a simple reason. It prints data in a new line.
Now, let's take a look at various examples of using format sequences.
Print Inode number only (%i)
To print the inode number, you'll have to use the
%i as format sequence:
stat --printf='Inode Number: %i\n' Filename
Display access time (%x)
To print the access time, you have to use
%x as format sequence:
stat --printf='Access Time: %x\n' Filename
Combine multiple format sequences at once
If you want, you can also use multiple-format sequences without any additional flags. But I would recommend adding space and text to make info human-readable.
For example, here, I combined two data sequences to get the inode number and access time:
stat --printf='Inode Number: %i\nAccess Time: %x\n' Chrome.deb
To learn more about format sequences, I would recommend checking out the help page of the stat command:
Learn more about timestamps in Linux
If you're new to Linux, timestamps may confuse you and to cater to this situation, we made a detailed guide with respect to the stat output:
I hope you will no longer find using the stat command a complex task.