Stat Command in Linux – Use + Examples

Last updated: April 5, 2022

Linux files and directories have multiple metadata that are useful to perform various operations on these files and directories. There are several ingenious ways to list these metadata. One such Linux command is stat, which is used to display default file metadata information about the file.

In this tutorial, we learn about stat command in Linux and show you how to use this command with practical examples.

Prerequisites

  • A working Linux computer.
  • Basic understanding of the command-line interface.
  • Root or normal user access to run the command examples.

Linux Stat Command

The stat command in Linux provides detailed information about the file system and files. This command displays important file information, such as file size in bytes, user ID, group ID, access rights, access time, and file birth time. Stat options can also display file system information. We can think of the stat as an improved form of the ls -l command.

Every Linux file and directory has an inode entry, which stores the file’s metadata. The stat command provides all the information about the file that is stored in the inode. This command provides a range of options that you can use to display different types of information about a file or directory.

Linux Stat Command Syntax

The basic syntax of the stat command is:

stat [options] file_name

How to Use Stat Command

You can use the stat command followed by a filename to display critical information about the particular file. By default, the stat command displays the detailed status of the specified file. You can also specify options to filter out the results.

When you use the stat command without any options, it provides the following information about the specified file:

stat users.txt
Default output of stat command
Figure 1

When we do not specify any option with the stat command, we get the following information:

  • File: Name of the file. If the specified file is a symbolic link, then the result will be a symlink, not the file.
  • Size: Size of the file in bytes.
  • Blocks: Number of blocks allocated to the file.
  • IO Block: Size of each block in bytes.
  • File type: Type of the file, which can be a regular file, symbolic link, directory, or special file.
  • Device: Device number in hexadecimal format.
  • Inode: File inode number.
  • Links: Number of hard links or soft links to the file.
  • Access: File access privileges in numeric and symbolic methods.
  • Uid: User ID and name of the owner.
  • Gid: Group identifier and user name of the owner.
  • Access: Timestamp of the time of last access.
  • Modify: Timestamp of the last time the file was last modified.
  • Change: Timestamp of the last time the file’s attribute or content was last changed.
  • Birth: Timestamp of the time of file birth.

About the Timestamps

Linux keeps precise records of the times in which files are accessed, modified, or their metadata are updated. These records are known as timestamps.

Linux records the following three timestamps for files:

  • Access timestamp (atime) - represents the last time when a particular file was accessed.
  • Modified timestamp (mtime) - represents the last time when a particular file’s content was modified.
  • Change timestamp (ctime) - represents the last time when a particular file’s metadata was modified. 

Modify and Change timestamps can be confusing because they will be the same if only the file content is updated. However, if you change file attributes such as file permissions, then only the change timestamp will be updated.

The timestamps are displayed with the time zone offsets. In Figure 1: at the end of each timestamp you can see +0530. The filesystem keeps timestamps in UTC and the stat command converts them to the local time zone. +0530 indicates the timezone is five hours 30 minutes ahead of UTC.

Stat Command Examples

In the following examples, we will describe stat command options and their output.

File System Information

Use the -f option with the stat command to display file system status and other information about a file system on which the file resides:

stat -f /boot
stat to display file system information of /boot

This output displays the following information:

  • File: Name of the file.
  • ID: System ID in hex format.
  • Namelen: Maximum length of the filename.
  • Type: Type of the file system.
  • Block size: Size of each block in the file system.
  • Blocks Total: Total number of file system blocks.
  • Block Free: Total number of free blocks.
  • Available: Total number of free blocks available to non-root users.
  • Inodes Total: Total number of inodes.
  • Inodes Free: Total number of free inodes.

Display Information of Multiple Files

Specify the list of file names separated by a blank space to display detailed information about multiple Linux files using the stat command:

stat users.txt updates.txt
using stat display informaiton about multiple files

The command displays information about each file in the sequence. Information about the first file is displayed first, the second file is displayed after that, and so on.

Symbolic Link File

If we specify symbolic links with the stat command, then the command displays information about the symlinks instead of the original file:

stat employees.txt
display symlink file information using stat command

The first line "File: employees.txt -> users.txt" indicates employees.txt is pointing to the original file users.txt. In the second line, the last column shows the file employees.txt is a symbolic link

A hard link file is treated as a regular Linux file. The following is the stat output of a hard link.

stat output of hard link file

Customize Output

If you do not prefer to use the default output format of the stat command, then you can customize it using different options:

stat --format=’%n’ users.txt
example showing stat command format

The %n format sequence list the name of the file.

Here we specify the format sequence to change the output. You can specify multiple format sequences, such as:

stat --format=’%n:%a:%B’ users.txt

If we specify multiple files with –-format, then the output is automatically displayed in separate lines for each file.

The following sequences are supported for files with the stat command:

Format sequenceDescription
%aAccess rights in Octal
%AAccess rights in symbolic format
%bNumber of allocated blocks
%BSize of blocks in bytes
%dDevice number in decimal format
%DDevice number in hexadecimal
%fRaw mode in hexadecimal
%FFile type
%gGroup id of the file owner
%GGroup name of the file owner
%hNumber of hard links
%iInode number
%mMount point
%nName of the file
%NName of the file in single quotation marks
%oBest I/O transfer size in bytes
%sTotal size in bytes
%tMajor device type in hex for special files
%TMinor device type in hex for special files
%uUser id of the file owner
%UUser name of the file owner
%wTime of file creation in date format, or – (hyphen) if unknown
%WTime of file creation in seconds since Epoch, or 0 (zero) if unknown
%xTime of last file access in human-readable form
%XTime of last file access in seconds since Epoch
%yTime of last data modification in human-readable form
%YTime of last data modification in seconds since Epoch
%zTime of last file status change in human-readable format
%ZTime of last file status change in seconds since Epoch

Some of these format sequences are also supported for the file systems, but the output may be different when a file system is specified as a command argument.

For example -f --format='%a' shows the number of free data blocks available to non-root users in the filesystem to which the file belongs.

stat command format for filesystem using -f option

The following format sequences are supported for file systems with the stat command:

Format sequence Description
%aNumber of free data blocks available to non-root users
%bNumber of total data blocks
%cNumber of total inodes
%dNumber of free inodes
%fNumber of free data blocks
%iFile system ID in hexadecimal format
%lMaximum filename length
%nFile name
%sSize of blocks for optimum writing
%SSize of blocks for block counts
%tType of file system in hexadecimal format
%TType of file system in a human-readable format

You can use these format sequences with either -–format or --printf. However, printf does not automatically add a new line or tab to the output. For example:

stat –-format=”File %N has inode number %i and it is a %F.”

Difference between Stat and Ls -l

Both stat and ls commands display information about files or directories, however, they are different in their core functionalities and the output can be different.

The ls command displays file permissions, file ownership, group name, size, name, and last modified date. The stat command prints additional information such as the size of blocks, number of links, access time, inode number, device ID, Uid, Gid, time of last access, modify timestamp, and file creation date.

Conclusion

In this tutorial, we learned how to use the stat command in Linux to display metadata information about a file or file system. This is a built-in command-line tool that works with all major Linux distributions, such as Debian, Ubuntu, Red Hat, CentOS Steam, and Fedora.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Comments Off on How to Articles