Beginners Guide for Stat Command in Linux

Linux TLDR
Last Updated:
Reading time: 3 minutes

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

DescriptionStat
Difficulty LevelLow
Root or Sudo PrivilegesNo
Host System and ArchitectureUbuntu 22.10 (x64)
OS Compatibility Ubuntu, Manjaro, Fedora, etc.
Prerequisitesstat
Internet RequiredNo
Discussed Tools in this Article

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:

Checking the properties of a file
Checking the properties of a file

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:

Checking the minimal file information
Checking the minimal file information

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:

Examining the referenced file, filesystem-related data
Examining the referenced file, filesystem-related data

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:

Checking the property of a symbolic link
Checking the property of a symbolic link

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:

Checking the properties of the actual file
Checking the properties of the actual file

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:

Using custom formatting with the stat command
Using custom formatting with the stat command

The following is a description of all the formats used in the above command.

FormatsDescription
%UUser name of the owner
%GGroup name of owner
%BThe size, in bytes, of each block reported by the %b
%CSELinux security context string
%zTime of last status change, human-readable
%FFile 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

FormatsDescription
%aAccess rights in octal (note ‘#’ and ‘0’ printf flags)
%AAccess rights in human readable form
%bNumber of blocks allocated (see %B)
%BThe size in bytes of each block is reported by %b
%CSELinux security context string
%dDevice number in decimal
%DDevice number in hex
%fRaw mode in hex
%FFile type
%gGroup ID of owner
%GGroup name of owner
%hNumber of hard links
%inode number
%mMount point
%nFile name
%NQuoted file name with dereference if symbolic link
%oOptimal I/O transfer size hint
%sTotal size, in bytes
%tMajor device type in hex, for character/block device special files
%TMinor device type in hex, for character/block device special files
%uUser ID of owner
%UUser name of owner
%wTime of file birth, human-readable; – if unknown
%WTime of file birth, seconds since Epoch; 0 if unknown
%xTime of last access, human-readable
%XTime of last access, seconds since Epoch
%yTime of last data modification, human-readable
%YTime of last data modification, seconds since Epoch
%zTime of last status change, human-readable
%ZTime of last status change, seconds since Epoch

The following is a list of valid file format sequences for file systems.

FormatsDescription
%aFree blocks available to non-superuser
%bTotal data blocks in file system
%cTotal file nodes in file system
%dFree file nodes in file system
%fFree blocks in file system
%iFile system ID in hex
%lMaximum length of filenames
%nFile name
%sBlock size (for faster transfers)
%SFundamental block size (for block counts)
%tFile system type in hex
%TFile 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.