The stat command is used in Linux/Unix system to display detailed information about files and file systems. It is most commonly used to get file timestamps.
The Linux ls command usually gives you basic details about a file whereas stat command prints additional information about the file pulled from the inode.
In this guide, we take a closer look at the stat command and its options through examples.
Syntax:
$ stat <OPTION> <FILENAME>
1) Check the status of a file
To display a file status such as size, inode number links, and file timestamps, run:
$ stat output.txt
Outputs the following information:
File - This is the file name.
Size - This is the size of the regular file in bytes.
Blocks - The number of allocated block designated for the file.
IO Block - The size in bytes of every block.
File type - Specifies what type the file is (Whether a regular file, symbolic link etc).
Device - This is the device number either in decimal or hexadecimal format.
Inode - Displays inode number.
Links - Specifies the number of hard links.
Access - This presents the file permissions in numeric or symbolic format.
Uid - This is the User ID & name of the owner.
Gid - The Group ID & name of the owner.
Context - This describes the SELinux security context.
Access - Points to the last time a file was accessed.
Modify - Points to last time a file’s content was modified.
Change - This is the last time the file’s metadata was changed.
2) Check filesystem status
To prints out the filesystem status on which the file resides instead of giving information about the regular file, use -f
or --file-system
option.
For example:
$ stat -f output.txt
Outputs the following information:
File -Describes the file's name.
ID - Specifies the system ID in hexadecimal.
Namelen - Specifies the maximum file length.
Fundamental block size - This is the size of each block on a file system.
Blocks:
Total - This points to the total number of blocks in the file system.
Free - Number of free blocks remaining in the file system.
Available - The number of free blocks available to non-root users.
Inodes:
Total - This is the total number of inodes in the system.
Free - This is the number of free inodes available.
4) To follow symlinks
The stat command does not follow symlinks by default. When you run it on a symlink, the output comprises information about the symlink but not the file it points to.
$ stat /usr/share/zoneinfo/America/Cayman
To follow the symlink and print out information about the file it points to, use the -L option as shown:
$ stat -L /usr/share/zoneinfo/America/Cayman
5) Custom output
Instead of printing the default custom format, you can use the --printf
or --format
options to customize the output of stat command.
Using --printf, you have to use \n to print in newline two or more file operands. For example to print device and inode number:
stat --printf='%d:%i\n' /usr /etc
Output 2048:63 2048:128001
Using --format it prints newline by default, for example:
stat --format=%d:%i /usr /etc
Output 2048:63 2048:128001
To print file name and last data modification time, type:
stat --printf='Name: %n\nThe time of last data modification: %y\n' /usr
Output Name: /usr The time of last data modification: 2020-09-28 21:47:27.451120000 +0000
To print user name of the owner, file type and total size in bytes, run:
stat --format="%U,%F,%s" /usr
Output stat --format="%U,%F,%s" /usr root,directory,4096
6) Display information in terse form
To print information in terse format, use the -t
option, which is useful for parsing by other tools.
Conclusion
The stat is a handy command to check file timestamps such as file modification or acess time. In this guide, we have covered the stat command in Linux and highlighted a few example usages.