Putorius
Basic Commands

Stat Command - Display File Information on the Command Line

While working on the Linux command line you interact with a lot of different files. Each of these files has attributes attached to them. These attributes contain details about the files size, permissions, modification time, and an assortment of other information. There are a multitude of reasons that you might need to the print the attributes of a file. This is where the Linux stat command comes in.

What is the Stat Command?

The stat command is part of the GNU Core Utilities which comes pre-installed with almost all UNIX and Linux systems. It is used to print the attributes of a file to standard output (STDOUT). In this article we will discuss how to use the stat command, using it's options and formatting the output.

Basic Syntax of Stat Command

The basic syntax that the stat command uses a simple structure. You only need to provide the name of the file as an argument to the command. The general syntax is:

stat [OPTION] ...filename...

Now, let us see how the actual command works in a real situation. Let's use it to display information about myfile.txt.

Screenshot of the stat command being used

Passing a filename as an argument is all that is required. However, there are several options available to expand the usefulness of the utility.

Stat Command Output Explained

The screenshot above is typical of the output you would see using the stat command. Let's break down the output, label by label.

Stat Command Options

Now that you know how to fetch the basic information of any file, it is time to see what other options the valuable stat command provides. The command could be used to get a specific piece of information instead of all the information. It can also be used to fetch details about a file system instead of a file. Additionally, the output could be modified into a more readable format for ease of use in other programs. Here are some of the options you could try your hands on.

Show Filesystem Information

You can use the -f (--filesystem) option to pull information about a filesystem instead of a file.

[savona@putor ~]$ stat -f /tmp
   File: "/tmp"
     ID: 0        Namelen: 255     Type: tmpfs
 Block size: 4096       Fundamental block size: 4096
 Blocks: Total: 2037690    Free: 2037679    Available: 2037679
 Inodes: Total: 2037690    Free: 2037652

Follow Symbolic Link / Dereference

If you run the stat command on a link, it will give you the information about that symlink, not the file it references.

[savona@putor ~]$ stat myfilelink 
   File: myfilelink -> myfile.txt
   Size: 10            Blocks: 0          IO Block: 4096   symbolic link
 Device: fd02h/64770d    Inode: 13670759    Links: 1
...OUTPUT TRUNCATED...

Using the -L (--dereference) option will tell stat to follow the link and give you the information on the file the link references.

[savona@putor ~]$ stat -L myfilelink 
   File: myfilelink
   Size: 890           Blocks: 8          IO Block: 4096   regular file
 Device: fd02h/64770d    Inode: 13640825    Links: 1
...OUTPUT TRUNCATED...

Use Terse Output / Remove Labels

The -t (--terse) output is all the same information, but without the labels and formatting. This is useful when using the output in scripts that need to parse specific information.

[savona@putor ~]$ stat -t myfile.txt 
 myfile.txt 890 8 81b4 1000 1000 fd02 13640825 1 0 0 1572312639 1568509270 1572312646 1568509270 4096 unconfined_u:object_r:user_home_t:s0

Format The Stat Command Output

The last option is -c (--format). This allows you to use format sequences to show just the exact information you need.

The syntax for using formatting is using the -c switch followed by the desired sequence and then the filename (or filesystem name) as an argument.

Here is an example of formatting the output to show only the name of the file owner.

[savona@putor ~]$ stat -c %U myfile.txt 
 savona 

Formatting Output of the Stat Command

There are many different formats you could use to display the information on your file. Here are some examples of the most common format sequences.

Display the Inode Number of a File

[savona@putor ~]$ stat -c %i myfile.txt 
 13640825

Show the File Type of a File

[savona@putor ~]$ stat -c %F myfile.txt 
 regular file

Same example with a symbolic link to show the difference:

[savona@putor ~]$ stat -c %F myfilelink 
 symbolic link

Show Filesystem Type

[savona@putor ~]$ stat -f -c %T /vault
 smb2

Combining Format Sequences

You can combine any number of formatting sequences in a single command to get the exact information you want. Here we use a few different sequences to get the owner name, group name of owner and permissions of myfile.txt.

stat -c "%U %G %A" myfile.txt

NOTE: If you put spaces between your sequences, you will have to wrap it in quotes.

You can even use a custom seperator. For example, let's say you wanted to dump the same information seperated by a comma. This may be useful if you want to copy this data into a spreadsheet as comma separated (CSV) format.

[savona@putor ~]$ stat -c "%U,%G,%A" myfile.txt 
 savona,savona,-rw-rw-r--

Another example using colons:

[savona@putor ~]$ stat -c "%U:%G:%A" myfile.txt 
 savona:savona:-rw-rw-r--

For a full list of valid formatting sequences see the man page (linked in the resources section) or use stat --help.

Conclusion

The examples above should guide you towards a better understanding of the stat command. With several command line options and formats out there, it is best if you use these examples and apply them to the files on your system to get more hands-on knowledge. Continued learning might help you fetch the exact file or file system details efficiently in real-time operations.

Resources and Links

Exit mobile version