Linux stat Command with Examples

August 11, 2023

Introduction

The stat command prints details about files and file systems. The tool provides information on who the owner is, modification dates, access permission, size, type, etc.

The utility is crucial for troubleshooting, getting information about a file before making changes to it, and routine file and system administration tasks.

This article explains all about the Linux stat command using practical examples.

Linux stat command with examples

Requirements

  • Linux system (this article uses Ubuntu 22.04).
  • Access to the terminal.

Linux stat Command Syntax

The basic stat syntax is:

stat [arguments] [filename]

Running stat without the [filename] parameter prints an error message indicating operands are missing:

stat
stat terminal output

The [filename] parameter is mandatory and represents the name of the file or file system users want to get information about. The following is an example of how to use stat to get information about MyFile in the Home directory:

stat MyFile
stat MyFile terminal output

The details in the output are:

  • File (MyFile). Prints the file name.
  • Size (518). Represents the file size in bytes.
  • Blocks (8). The number of blocks the file uses on the disk.
  • IO Block (4096). Each block size.
  • File type (regular). The type of file.
  • Device (803h/2051d). The device the file is stored on.
  • Inode (789026). The file inode number.
  • Links (1). The hard links number to the file.
  • Access ( 0664/-rw-rw-r--). The access permissions for the file.
  • Uid (1000/sara). The owner’s user ID.
  • Gid(1000/sara). The owner’s group ID.
  • Access (2023-08-03 10:12:32.585796414 +0200). The file's last accessed time.
  • Modify (2023-08-03 10:09:47.518441487 +0200). The file's last modified time.
  • Change (2023-08-03 10:09:47.566431565 +0200). The last file change time.
  • Birth (2023-08-03 10:09:47.518441487 +0200). The time the file was created.

Linux stat Command Examples

The stat command provides different outputs based on the arguments and variables used. Examples of how to use stat are in the sections below.

Example 1: Print File System Info

The stat command with the -f argument prints information about the file system a file or directory belongs to.

For example, show details about the file system the file MyFile is on:

stat -f MyFile
stat -f MyFile terminal output

The output includes details such as the file system ID, the type, the size of each block on the file system, and the total number of blocks on the file system.

To print information about the file system a particular directory is on, use the following syntax:

stat -f /directory/path/

For example, the following command outputs info about the file system containing ./Desktop/Documents/NewDirectory:

stat -f ./Desktop/Documents/NewDirectory
stat -f directory path terminal output

Example 2: Get Details from Multiple Files

To print info on multiple files, specify file names one after the other, separated by spaces. For instance, enter:

stat MyFile MyFile2
stat MyFile MyFile2 terminal output

The command shows details for the first mentioned file and then for the second.

Example 3: Get Symlink Info

symbolic link (symlink) is a file that points to another file or directory. It creates shortcuts to files and directories.

The stat command does not follow symlinks by default. Therefore, when running stat on a symlink, the output includes info about the symlink, not the file it points to.

For example, run stat with the symlink to Vim:

stat /usr/local/bin/vim
stat with symlink terminal output

To get information about the file the symlink points to, use the deference argument -L.

stat -L stat /usr/local/bin/vim
stat -L usr/local/bin/vim terminal output

The entire output displays info about Vim and not the symlink.

Note: The field file shows the symlink and not the file. This happens because stat was run with the symlink.

Example 4: Display Information in Terse Form

The stat command is also able to produce a more concise (terse) output. To accomplish this, run the following:

stat -t MyFile
stat -t MyFile terminal output

Unlike the stat MyFile output, the -t option shows only essential details about the file. The argument also removes formatting from the output, such as the line breaks and the spaces.

Example 5: Customize Output Format

The stat command prints out a lot of information about a file. The format operands and arguments allow users to customize the output and see details relevant to them.

The two arguments used to customize the stat output are:

  • -c (--format). Tells stat to use the specified format string instead of the default format.
  •  --printf. Similar to -c, but it allows the use of backslashes in the format string.

The following table lists the most commonly used format operands for files and directories: 

OperandFilesFile Systems
%aAccess rights in octal format.Free blocks available to non-superuser.
%AAccess rights in human-readable form. /
%bNumber of blocks allocated.Total data blocks in a file system.
%BThe size in bytes of each block. /
%c /Total data blocks in a file system.
%CThe raw mode in hex. /
%dDevice number in decimal.Total file nodes in a file system.
%DDevice number in hex. /
%f Raw mode in hex.Free file nodes in a file system.
%FFile type. /
%gGroup owner ID. /
%GGroup owner name. /
%hHard links number. /
%i Inode number.File system ID in hex.
%l /Free blocks in the file system.
%mMount point. 
%nFile name.File name.
%NQuoted file name with dereferencing if a symlink is used.
%oOptimal I/O transfer size hint.
%sTotal size.Block size.
%S /Fundamental block size.
%tMajor device type in hex.File system type in hex.
%TMinor device type in hex.File system type in human-readable form.
%uUser ID. /
%UUser name. /
%wFile birth time, human-readable. /
%W File birth time, seconds since Epoch. /
%xLast access time, human-readable. /
%XLast access time, seconds since Epoch. /
%yLast data modification time, human-readable. /
%Y   Last data modification time, seconds since Epoch. /
%z Last status change time, human-readable. /
%ZLast status change time, seconds since Epoch. /

The general syntax for formatting the stat output is:

stat [argument] = "FORMAT" [filename]

For example, the following command prints only the MyFile inode number:

stat -c=%i MyFile
stat c %i MyFile terminal output

Accomplish the same, but with --printf:

stat --printf='%i MyFile
stat --printf %i MyFile terminal output

The inode, in this case, is not in a new line. Therefore, insert a newline character at the end with a backslash escape character:

stat --printf='%i\n' MyFile
stat --printf %i n MyFile terminal output

The following command prints the file type, user ID, and last modified time:

stat --format=%F:%u:%y MyFile
stat --format multiple operands terminal output

The output shows:

  • Regular file. Displays the file type.
  • 1000. Shows the file owner's user ID.
  • 2023-08-03 10:09:47.518441487 +0200. Represents the file's last modification time.

Conclusion

After reading this article, you are familiar with the Linux stat command through examples.

Next, learn about the ls command, which lists the contents of a directory but also provides info about files.

Was this article helpful?
YesNo
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
Next you should read
How to Use the vmstat Command
March 18, 2021

The vmstat command is a Linux monitoring utility used to see various system statistics and to diagnose performance bottleneck issues. Learn how to use the vmstat...
Read more
Netstat Command in Linux - 28 Commands with Examples
January 28, 2021

The netstat tool gives an overview of network activities and display open or have established connections. Read about the most...
Read more
Linux File Command: How to Determine File Type in Linux
March 3, 2022

The file command in Linux determines the type of a file. It doesn't take the file extension into account. Learn how to use the file command in this tutorial...
Read more
How to Use fsck Command to Check and Repair Filesystem
May 14, 2020

If you want to keep your filesystems clean and without errors, you need to scan it on regular basis. In this guide, you can find all fsck...
Read more