Linux stat Command Tutorial for Beginners (5 Examples)

Sometimes, while working on the command line in Linux, you need to know more about a file. For example, you may want information like file’s size, inode number, access permissions, time of last access or modification, and more. You’ll be glad to know that there exists a command line utility stat that provides all this information in one go.

In this tutorial, we will discuss the basics of stat using some easy to understand examples. But before we do that, it’s worth mentioning that all examples mentioned here have been tested on Ubuntu 16.04LTS.

Linux stat command

The stat command in Linux allows you to view the status of a file (or even a file system). Here’s the syntax of the command:

stat [OPTION]… FILE…

Following are some Q&A-styled examples that will give you a good idea on how stat works.

Q1. How to use stat command?

The tool’s basic usage is very easy – all you have to do is to run the ‘stat’ command with the name of the file you want to know more about.

stat [filename]

For example:

stat test.txt

How to use stat command

So you can see details like file name, size, type of file, access permissions, UIDs and GIDs, as well as Access/Modify/Change times.

Q2. How to make stat display information in terse form?

The terse form of output makes it easy for other programs to parse the output. To make ‘stat’ output information in this form, use the -t command line option.

stat -t test.txt

How to make stat display information in terse form

Q3. How to make stat output info in custom format?

For this, use the –format option. This option requires you to enter a valid format sequence, and a lot of those are available. For example, to access permissions/rights in human readable form, use %A.

For example:

stat –format=%A test.txt

produces the following output:

How to make stat output info in custom format

Similarly, you can use %n for file name, %s for size, and %U for user name of the file owner.

Stat command output

Q4. How is –format option different from –printf?

At the core, both options do the same thing. The difference lies in the way outputs are produced. While –format outputs a newline after each use, in case of –printf, no newline is added. Here’s how the man page explains both:

       -c  --format=FORMAT
              use the specified FORMAT instead of the default; output  a  new?
              line after each use of FORMAT

       --printf=FORMAT
              like  --format, but interpret backslash escapes, and do not out?
              put a mandatory trailing newline; if you want a newline, include
              \n in FORMAT

There’s a dedicated option for this as well: -L. So if the input file is a link, and you want stat to follow it when the command runs, run it in the following way:

stat -L [filename]

Conclusion

It’d be wrong to say stat doesn’t have a learning curve, but the amount of effort the tool demands isn’t much, as the number of command line options it offers are limited. We have already discussed the majority of them here. For more information, head to the tool’s man page.

Share this page:

نوشته های مشابه