stat
Display file or filesystem status
TLDR
Display properties about a specific file such as size, permissions, creation and access dates among others
Display properties about a specific file such as size, permissions, creation and access dates among others without labels
Display information about the filesystem where a specific file is located
Show only octal file permissions
Show the owner and group of a specific file
Show the size of a specific file in bytes
SYNOPSIS
stat [OPTION]... FILE...
PARAMETERS
-L, --dereference
Follow symbolic links. If FILE is a symbolic link, stat will report on the file it points to, not the link itself. (This is often the default behavior).
-P, --no-dereference
Do not follow symbolic links. If FILE is a symbolic link, stat will report on the link itself, not the file it points to.
-f, --file-system
Display file system status instead of file status. When used, arguments are interpreted as files within a file system, and stat reports on the file system hosting them.
-c FORMAT, --format=FORMAT
Use the specified FORMAT string instead of the default output format. FORMAT can include printf-like escape sequences to extract specific attributes.
-t, --terse
Print the information in a terse (condensed) form. This option is often used in conjunction with --format for scripting purposes.
-Z, --context
Print the SELinux security context of the file or file system.
--version
Display version information and exit.
--help
Display a help message and exit.
DESCRIPTION
The stat command in Linux is a powerful utility used to retrieve and display detailed status information about files, directories, or file systems. Unlike simpler commands like ls -l which provide a basic overview, stat delves deeper, offering insights into various metadata attributes.
It can report on parameters such as access permissions (mode), inode number, device ID, number of hard links, user and group ownership (UID/GID and names), total size in bytes, allocated block count, optimal I/O block size, and a range of timestamps including last access, last modification, last status change, and crucially, the file's creation (birth) time where supported by the underlying file system.
This utility is indispensable for system administrators, developers, and anyone performing shell scripting, as it allows for precise data extraction for automation, troubleshooting permission issues, or analyzing file activity. Internally, stat leverages the stat(), fstat(), and lstat() system calls to query the file system for the requested metadata. Its most distinctive feature is the highly customizable output format, enabling users to specify exactly which pieces of information they need and in what format, making it ideal for machine-readable output and integration into scripts.
CAVEATS
- Birth Time (%w): The 'birth' (creation) time is not universally supported by all file systems (e.g., older ext3/4 configurations may not store it by default). If unavailable, it may report 0 or an approximated value.
- Timestamp Precision: The precision of timestamps (access, modify, change) can vary depending on the file system type and kernel configuration.
- Symbolic Link Behavior: By default, stat dereferences symbolic links, meaning it provides information about the target file. To get information about the symbolic link itself, you must explicitly use the -P or --no-dereference option.
COMMON FORMAT SEQUENCES FOR --FORMAT/--PRINTF
When using the --format or --printf options, these are some frequently used sequences:
%n: File name
%s: Total size in bytes
%b: Number of blocks allocated (512B units)
%B: Size in bytes of each block
%d: Device number (in decimal)
%D: Device number (in hex)
%i: Inode number
%h: Number of hard links
%u: User ID of owner
%U: User name of owner
%g: Group ID of owner
%G: Group name of owner
%a: Access rights in octal
%A: Access rights in human-readable form (e.g., -rw-r--r--)
%F: File type (e.g., regular file, directory, symbolic link)
%x: Time of last access (in seconds since Epoch)
%X: Time of last access (human-readable)
%y: Time of last modification (in seconds since Epoch)
%Y: Time of last modification (human-readable)
%z: Time of last status change (in seconds since Epoch)
%Z: Time of last status change (human-readable)
%w: Time of file creation (birth time, in seconds since Epoch)
%W: Time of file creation (birth time, human-readable)
HISTORY
The stat command's core functionality is rooted in the Unix stat() system call, which has been a fundamental part of the Unix/Linux kernel API for retrieving file metadata since its early days. The command-line utility itself is a standard component of the GNU Core Utilities, a collection of essential file, shell, and text manipulation utilities prevalent on most Linux distributions. Its development has generally mirrored the evolution of file system capabilities, such as the introduction of more precise timestamps or extended attributes, which the command then exposes to users.