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.
- 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:
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
-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
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
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:
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.
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
%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:
|Access rights in Octal|
|Access rights in symbolic format|
|Number of allocated blocks|
|Size of blocks in bytes|
|Device number in decimal format|
|Device number in hexadecimal|
|Raw mode in hexadecimal|
|Group id of the file owner|
|Group name of the file owner|
|Number of hard links|
|Name of the file|
|Name of the file in single quotation marks|
|Best I/O transfer size in bytes|
|Total size in bytes|
|Major device type in hex for special files|
|Minor device type in hex for special files|
|User id of the file owner|
|User name of the file owner|
|Time of file creation in date format, or – (hyphen) if unknown|
|Time of file creation in seconds since Epoch, or 0 (zero) if unknown|
|Time of last file access in human-readable form|
|Time of last file access in seconds since Epoch|
|Time of last data modification in human-readable form|
|Time of last data modification in seconds since Epoch|
|Time of last file status change in human-readable format|
|Time 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.
-f --format='%a' shows the number of free data blocks available to non-root users in the filesystem to which the file belongs.
The following format sequences are supported for file systems with the stat command:
|Number of free data blocks available to non-root users|
|Number of total data blocks|
|Number of total inodes|
|Number of free inodes|
|Number of free data blocks|
|File system ID in hexadecimal format|
|Maximum filename length|
|Size of blocks for optimum writing|
|Size of blocks for block counts|
|Type of file system in hexadecimal format|
|Type of file system in a human-readable format|
You can use these format sequences with either
--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.
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.