How to Use the Linux Command stat and Its 4 Options

The Linux Command stat is very useful when you need to check detailed information about a file or directory. In this article, we will explain how to use the stat command, its main options, and how to apply it in real-life scenarios.

What is the Linux Command stat?

The stat command is a Linux command that shows the status of a file or file system. It provides various information such as file size, permissions, last access time, modification time, and creation time.

Basic Usage

To use the stat command, enter the following in the terminal:

stat [filename or directory]
ShellScript

For example, to check the status of the file example.txt in the current directory, enter:

stat example.txt
ShellScript

Executing this command will output the following result.

Figure 1. Linux Command stat: Execution Result
Figure 1. Linux Command stat: Execution Result

Main Options

The stat command provides various options to selectively display the information you need. Let’s look at some main options.

The -L option allows you to view the original file information if the file is a symbolic link. Instead of checking the link itself and then using the stat command to find the original, you can use the -L option to directly view the original file’s information.

stat -L filename
ShellScript

In the figure below, using the stat command without the -L option shows how the symbolic link is connected and indicates that the file is a symbolic link. On the other hand, using the -L option shows the contents of the original file original_file.

Figure 2. Linux Command stat: Viewing Original File Attributes with -L Option
Figure 2. Linux Command stat: Viewing Original File Attributes with -L Option

Below is the information for original_file. It confirms the same result when using the stat -L option as shown above.

Figure 3. Linux Command stat: Viewing Original File
Figure 3. Linux Command stat: Viewing Original File

-c, –format Option (Specify Format)

The -c and --format options allow you to specify the output format. However, special characters used with backslashes in the output format string are not interpreted. The output ends with a newline character (\n) added at the end.

Here is an example that sequentially shows the file name, size, and permissions.

stat -c "Name: %n   Size(bytes): %s   Permissions: %A" example.txt
ShellScript

In this command, %n represents the file name and %s represents the file size. The result shows the file name and file size as follows.

Figure 4. Linux Command stat: Specify Output Format (-c, --format Option)
Figure 4. Linux Command stat: Specify Output Format (-c, –format Option)

–printf Option

The --printf option can use the same format types as the -c or --format options. However, unlike the -c or --format options, it does not add a newline after printing the string format. Therefore, you need to add a newline character if you want a newline. Additionally, using the –printf option allows for the use of special characters like \n and \t with backslashes.

stat --printf="Name: %n\nSize: %s\nPermissions: %A" example.txt
ShellScript

In the example below, because the newline character was not added, the next command prompt appears immediately after the output string.

Figure 5. Linux Command stat: Specify Output Format (--printf Option)
Figure 5. Linux Command stat: Specify Output Format (–printf Option)

-t Option (Terse Format)

The -t option summarizes the output into a single line. This is useful for easily comparing multiple files.

stat -t example.txt
ShellScript

The result is as follows.

Figure 6. Linux Command stat: One-Line Summary with -t Option
Figure 6. Linux Command stat: One-Line Summary with -t Option

The above output is equivalent to using the -c option as shown below.

stat -c "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %W %o" original_file
ShellScript

Output Format Types

Below are the format sequences you can use for output.

The valid format sequences for files (without --file-system):

%a permission bits in octal (note '#' and '0' printf flags)
%A permission bits and file type in human readable form
%b number of blocks allocated (see %B)
%B the size in bytes of each block reported by %b
%C SELinux security context string
%d device number in decimal (st_dev)
%D device number in hex (st_dev)
%Hd major device number in decimal
%Ld minor device number in decimal
%f raw mode in hex
%F file type
%g group ID of owner
%G group name of owner
%h number of hard links
%i inode number
%m mount point
%n file name
%N quoted file name with dereference if symbolic link
%o optimal I/O transfer size hint
%s total size, in bytes
%r device type in decimal (st_rdev)
%R device type in hex (st_rdev)
%Hr major device type in decimal, for character/block device special files
%Lr minor device type in decimal, for character/block device special files
%t major device type in hex, for character/block device special files
%T minor device type in hex, for character/block device special files
%u user ID of owner
%U user name of owner
%w time of file birth, human-readable; - if unknown
%W time of file birth, seconds since Epoch; 0 if unknown
%x time of last access, human-readable
%X time of last access, seconds since Epoch
%y time of last data modification, human-readable
%Y time of last data modification, seconds since Epoch
%z time of last status change, human-readable
%Z time of last status change, seconds since Epoch

Valid format sequences for file systems:

%a free blocks available to non-superuser
%b total data blocks in file system
%c total file nodes in file system
%d free file nodes in file system
%f free blocks in file system
%i file system ID in hex
%l maximum length of filenames
%n file name
%s block size (for faster transfers)
%S fundamental block size (for block counts)
%t file system type in hex
%T file system type in human readable form

Real-Life Applications

File Management and Backup

You can check the last access time and modification time of a file to determine how frequently it is used. This is useful for organizing old files or planning a backup strategy.

Writing Scripts

Using the stat command, you can write various scripts. For example, you can write a script that finds files above a certain size and lists them.

#!/bin/bash
for file in *
do
  if [ $(stat -c %s "$file") -gt 50 ]
  then
    echo "$file"
  fi
done
ShellScript

This script lists files in the current directory that are larger than 50 bytes. Among the files below, only check.sh and example.txt, which are larger than 50 bytes, are listed.

Figure 7. Linux Command stat: Application Using Shell Script
Figure 7. Linux Command stat: Application Using Shell Script

Summary

The stat command is a very useful tool for checking detailed information about files or directories in a Linux system. By utilizing various options, you can efficiently obtain the necessary information, which can be applied to file management, script writing, system monitoring, and more. Effectively using the stat command can help you manage your Linux system more efficiently.

References

Leave a Comment