The stat command is used to get detailed information about a file or filesystem, such as the file size, blocks, IO blocks, device type, SELinux security context string, Inode, permissions, and modification date.
Tutorial Details
Description | Stat |
Difficulty Level | Low |
Root or Sudo Privileges | No |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | stat |
Internet Required | No |
Syntax of the Stat Command
The stat command takes two arguments: one is the option, and the other is the filename.
$ stat [OPTION] [FILENAME]
Displaying File Properties
If you don’t use any flags, the stat command will show the properties of a specific file by default. These properties include the file’s size, permissions, creation and access dates, and many more.
$ stat file.txt
Output:
You can specify multiple files by using the space as a separator, like “stat file1.txt file2.txt
“.
Displaying File Properties Without Labels
The “-t
” or “--terse
” flag will return the same result as the previous command by just ignoring the labels.
$ stat -t file.txt
Output:
Displaying Filesystem Information
The “-f
” or “--file-system
” flag will print information about the filesystem where a specific file is located.
$ stat -f file.txt
Output:
Dereference Symlinks
If you specify the symlinks to the stat command, it will give you information about the symlink instead of the actual file to which it points.
$ stat Documents/file.txt
Output:
However, you can use the “-L
” flag to retrieve information about the actual file or directory to which the symlink points.
$ stat -L Documents/file.txt
Output:
Custom Format to Display File Properties
You can use the “-c
” flag to specify the format that will be displayed in newlines.
However, you can also use the “--printf
” option, which enables interpreting if a backslash escapes the sequence without a trailing newline.
To enable the trailing newline, use the “\n
” in the format to print a new line.
$ stat --printf='%U\n%G\n%B\n%C\n%z\n%F\n' file.txt
Output:
The following is a description of all the formats used in the above command.
Formats | Description |
---|---|
%U | User name of the owner |
%G | Group name of owner |
%B | The size, in bytes, of each block reported by the %b |
%C | SELinux security context string |
%z | Time of last status change, human-readable |
%F | File type |
The above is not the complete list of formats that stat supports.
The following is the list of valid format sequences:
Valid Format Sequences
Formats | Description |
---|---|
%a | Access rights in octal (note ‘#’ and ‘0’ printf flags) |
%A | Access rights in human readable form |
%b | Number of blocks allocated (see %B) |
%B | The size in bytes of each block is reported by %b |
%C | SELinux security context string |
%d | Device number in decimal |
%D | Device number in hex |
%f | Raw mode in hex |
%F | File type |
%g | Group ID of owner |
%G | Group name of owner |
%h | Number of hard links |
%i | node number |
%m | Mount point |
%n | File name |
%N | Quoted file name with dereference if symbolic link |
%o | Optimal I/O transfer size hint |
%s | Total size, in bytes |
%t | Major device type in hex, for character/block device special files |
%T | Minor device type in hex, for character/block device special files |
%u | User ID of owner |
%U | User name of owner |
%w | Time of file birth, human-readable; – if unknown |
%W | Time of file birth, seconds since Epoch; 0 if unknown |
%x | Time of last access, human-readable |
%X | Time of last access, seconds since Epoch |
%y | Time of last data modification, human-readable |
%Y | Time of last data modification, seconds since Epoch |
%z | Time of last status change, human-readable |
%Z | Time of last status change, seconds since Epoch |
The following is a list of valid file format sequences for file systems.
Formats | Description |
---|---|
%a | Free blocks available to non-superuser |
%b | Total data blocks in file system |
%c | Total file nodes in file system |
%d | Free file nodes in file system |
%f | Free blocks in file system |
%i | File system ID in hex |
%l | Maximum length of filenames |
%n | File name |
%s | Block size (for faster transfers) |
%S | Fundamental block size (for block counts) |
%t | File system type in hex |
%T | File system type in human readable form |
That’s the end, sayonara!
Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.