Where to start First steps track · 15 stages
Linux Labs

stat — file metadata

4 min GNU coreutils

Full file information — size, permissions, owner, timestamps (atime/mtime/ctime), inode.

Cheat sheet

4 commands Printable version
  • stat file everything
  • stat -c '%a %U:%G %n' file permissions-owner-name
  • stat -c '%Y' file mtime (unix)
  • ls -lc file ctime instead of mtime

Overview

stat shows a file's metadata, which ls doesn't show: exact timestamps, inode, link count, permissions in octal, owner and group.

Indispensable when working out "why doesn't the service read this file" or "when was this changed". By default, stat on a symlink shows the link's own metadata, not the metadata of the file it points to — the -L flag is needed for the target's metadata.

Options

5

The -c flag sets a custom output format via placeholders (%a, %U, %s, and others) — without it, stat prints a fixed multi-line block that's inconvenient to parse in scripts. -L (--dereference) switches the output from the link itself to the file it points to; without it, checking permissions with stat on a symlink shows the link's permissions (usually 777), not the file's.

Option What it does Example
-c --format FORMAT Custom output format using placeholders (%a, %U, %s, and more). stat -c '%a %U:%G %n' file
-L --dereference Show metadata for the file a symlink points to, not the link itself. stat -L file
-f --file-system Show file-system information instead of file metadata. stat -f -c '%T' /
--printf FORMAT Like --format, but without an automatic trailing newline. stat --printf='%n %s байт\n' *.log
-t --terse Terse single-line output — convenient to parse with awk or cut. stat -t file

Nothing found. Try a shorter query.

Recipes

2

Examples

bash
stat file.conf                    # all the information
stat -c '%A %U:%G %s %n' file     # pick fields: permissions, owner:group, size, name
stat -c '%a' file                 # permissions in OCTAL (e.g. 644)
stat -c '%Y' file                 # mtime as unix time (handy in scripts)
stat -f -c '%T' /                 # the root filesystem's type
stat --printf='%n %s bytes\n' *.log

Common -c format fields:

Field Meaning
%n name
%s size in bytes
%A / %a permissions (rwx / octal)
%U / %G owner / group
%y / %Y mtime (human-readable / unix)
%i inode number
%h number of hard links

Three timestamps

  • atime (%x) — last access (read). Often disabled (noatime) for speed.
  • mtime (%y) — last content change. This is what ls -l shows.
  • ctime (%z) — last metadata change (permissions, owner, rename).
bash
ls -l   file    # shows mtime
ls -lc  file    # shows ctime
ls -lu  file    # shows atime

Troubleshooting

3

"I didn't touch the file, but mtime is fresh"

The file was rewritten (an editor saves in place) or a process appended to it. ctime shows whether permissions/owner were touched separately from the content.

ls -l shows a large size but no space is used

A sparse file. Compare stat -c '%s' (logical size) with %b×%B (blocks actually allocated) — see also disk-usage.

A symlink's permissions don't match expectations

Symlinks on Linux almost always show 777 — these permissions don't affect access to the target, which is checked on the target file itself. View them with stat -L file, not stat file.

See also

  • files-basics — Navigation and files — ls, cd, cp, mv, rm, cat, less, head, tail, copy-paste in the terminal.
  • chmod-chown — Changing permissions (chmod) and owner/group (chown) — numeric and symbolic syntax.
  • disk-usage — Where the space and inodes went — df, du, finding large files, pitfalls.
Loading…