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 system (this article uses Ubuntu 22.04).
- Access to the terminal.
Linux stat Command Syntax
stat syntax is:
stat [arguments] [filename]
stat without the [filename] parameter prints an error message indicating operands are missing:
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:
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
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
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
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
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
The command shows details for the first mentioned file and then for the second.
Example 3: Get Symlink Info
A symbolic link (symlink) is a file that points to another file or directory. It creates shortcuts to files and directories.
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:
To get information about the file the symlink points to, use the deference argument
stat -L stat /usr/local/bin/vim
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
stat command is also able to produce a more concise (terse) output. To accomplish this, run the following:
stat -t MyFile
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
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:
statto 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:
|%a||Access rights in octal format.||Free blocks available to non-superuser.|
|%A||Access rights in human-readable form.||/|
|%b||Number of blocks allocated.||Total data blocks in a file system.|
|%B||The size in bytes of each block.||/|
|%c||/||Total data blocks in a file system.|
|%C||The raw mode in hex.||/|
|%d||Device number in decimal.||Total file nodes in a file system.|
|%D||Device number in hex.||/|
|%f||Raw mode in hex.||Free file nodes in a file system.|
|%g||Group owner ID.||/|
|%G||Group owner name.||/|
|%h||Hard links number.||/|
|%i||Inode number.||File system ID in hex.|
|%l||/||Free blocks in the file system.|
|%n||File name.||File name.|
|%N||Quoted file name with dereferencing if a symlink is used.||/|
|%o||Optimal I/O transfer size hint.||/|
|%s||Total size.||Block size.|
|%S||/||Fundamental block size.|
|%t||Major device type in hex.||File system type in hex.|
|%T||Minor device type in hex.||File system type in human-readable form.|
|%w||File birth time, human-readable.||/|
|%W||File birth time, seconds since Epoch.||/|
|%x||Last access time, human-readable.||/|
|%X||Last access time, seconds since Epoch.||/|
|%y||Last data modification time, human-readable.||/|
|%Y||Last data modification time, seconds since Epoch.||/|
|%z||Last status change time, human-readable.||/|
|%Z||Last 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
Accomplish the same, but with
stat --printf='%i MyFile
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
The following command prints the file type, user ID, and last modified time:
stat --format=%F:%u:%y MyFile
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.
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.