stat — Detailed File and Filesystem Information
Display all metadata of a file with stat — size, permissions, owner, timestamps and inode, plus format strings and filesystem status.
stat shows every piece of a file's metadata at a glance – far more than ls -l: the exact size in bytes, permissions in both symbolic and octal form, owner and group, inode number, and the three timestamps for access (atime), modification (mtime) and status change (ctime). With the -c option and format strings such as %s, %a or %U you build the output exactly as a script needs it. Important: the syntax shown here applies to GNU stat from coreutils on Linux. On macOS and BSD the format switch is -f (instead of -c) and uses entirely different placeholders – there, -f is also not the filesystem status. On Linux, -f shows the filesystem status instead of file info.
Basic Usage
stat <file> — Show all available information about a file.
stat index.htmlstat -f <filesystem> — Display filesystem status instead of file status.
stat -f /stat -L <symlink> — Follow symlinks and show info about the target file.
stat -L /usr/bin/python3stat <file1> <file2> — Show information for multiple files.
stat *.confFormat Strings — File Info
stat -c '%s' <file> — Show file size in bytes.
stat -c '%s' backup.tar.gzstat -c '%n %s' <file> — Show filename and size.
stat -c '%n %s' *.logstat -c '%A' <file> — Show human-readable permissions (e.g., -rwxr-xr-x).
stat -c '%A' script.shstat -c '%a' <file> — Show permissions as octal number (e.g., 755).
stat -c '%a' /etc/passwdstat -c '%U:%G' <file> — Show owner and group names.
stat -c '%U:%G' /var/log/syslogstat -c '%F' <file> — Show file type (regular file, directory, symlink, etc.).
stat -c '%F' /dev/nullstat -c '%i' <file> — Show inode number.
stat -c '%i' myfile.txtFormat Strings — Timestamps
stat -c '%y' <file> — Show last modification time (human-readable).
stat -c '%y' config.yamlstat -c '%x' <file> — Show last access time.
stat -c '%x' data.csvstat -c '%z' <file> — Show last status change time (ctime).
stat -c '%z' /etc/hostsstat -c '%w' <file> — Show file creation (birth) time if available.
stat -c '%w' readme.mdstat -c '%Y' <file> — Show modification time as Unix timestamp (seconds since epoch).
stat -c '%Y' deploy.logFilesystem Info
stat -f -c '%T' <path> — Show filesystem type.
stat -f -c '%T' /stat -f -c '%b %S' <path> — Show total blocks and block size.
stat -f -c '%b %S' /homestat -f -c '%a' <path> — Show available blocks for unprivileged users.
stat -f -c '%a' /tmpCommon Patterns
stat -c '%a %n' * — List permissions of all files in current directory.
stat -c '%a %n' /etc/*.confstat -c '%s %n' * | sort -n — List files sorted by size.
stat -c '%s %n' *.log | sort -nstat -c '%Y %n' * | sort -n | tail -5 — Show 5 most recently modified files.
stat -c '%Y %n' /var/log/* | sort -n | tail -5stat -c '%U %n' * | grep -v root — Find files not owned by root.
stat -c '%U %n' /usr/local/bin/* | grep -v root Conclusion
stat is your tool when ls -l does not tell you enough: exact bytes, all three timestamps and the inode at a glance. Its real strength shows with -c and format strings – with those you produce exactly the fields a script can process further, e.g. stat -c '%s %n' for a list sortable by size. When porting scripts, watch the platform: GNU stat uses -c with the placeholders shown here, whereas macOS/BSD use -f with a completely different format syntax – scripts meant to run on both must account for that (or use gstat from coreutils on macOS). For a quick overview reach for ls, for the content-based file type reach for file, and to change permissions reach for chmod.
Further Reading
- GNU Coreutils: stat invocation – official reference for all format strings
- Wikipedia: stat (system call) – background on the underlying file-status system call