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
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
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:
stat employees.txt
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.
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
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 sequence | Description |
---|---|
%a | Access rights in Octal |
%A | Access rights in symbolic format |
%b | Number of allocated blocks |
%B | Size of blocks in bytes |
%d | Device number in decimal format |
%D | Device number in hexadecimal |
%f | Raw mode in hexadecimal |
%F | File type |
%g | Group id of the file owner |
%G | Group name of the file owner |
%h | Number of hard links |
%i | Inode number |
%m | Mount point |
%n | Name of the file |
%N | Name of the file in single quotation marks |
%o | Best I/O transfer size in bytes |
%s | Total size in bytes |
%t | Major device type in hex for special files |
%T | Minor device type in hex for special files |
%u | User id of the file owner |
%U | User name of the file owner |
%w | Time of file creation in date format, or – (hyphen) if unknown |
%W | Time of file creation in seconds since Epoch, or 0 (zero) if unknown |
%x | Time of last file access in human-readable form |
%X | Time of last file access in seconds since Epoch |
%y | Time of last data modification in human-readable form |
%Y | Time of last data modification in seconds since Epoch |
%z | Time of last file status change in human-readable format |
%Z | 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.
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.
The following format sequences are supported for file systems with the stat command:
Format sequence | Description |
---|---|
%a | Number of free data blocks available to non-root users |
%b | Number of total data blocks |
%c | Number of total inodes |
%d | Number of free inodes |
%f | Number of free data blocks |
%i | File system ID in hexadecimal format |
%l | Maximum filename length |
%n | File name |
%s | Size of blocks for optimum writing |
%S | Size of blocks for block counts |
%t | Type of file system in hexadecimal format |
%T | Type 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.
Comments