Linux Command Reference ยท Files and Directories
Linux stat command
Reference for the Linux stat command: show size, permissions, links, inode and timestamps, build custom output with -c, and inspect file systems with -f.
stat prints everything the file system knows about a file except its content: size, type, permission bits, owner, number of hard links, inode number and the access, modify and change timestamps. Where ls -l gives a short summary, stat gives the raw facts.
Its real power for scripts is the -c option (or --printf), which lets you pick exactly the fields you need with format sequences such as %s for the size or %a for the octal permissions. That is far more robust than parsing ls output.
stat [OPTION]... FILE...
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
stat [-L] [-f] [-t] [-c FORMAT | --printf=FORMAT] FILE...
| Option | Description |
|---|---|
-c FORMAT, --format=FORMAT |
Print using a format string; a newline is added after each file. |
--printf=FORMAT |
Like -c, but interprets backslash escapes (\n, \t) and does not add a newline. |
-L, --dereference |
Follow symbolic links and report on the target. |
-f, --file-system |
Report on the file system that holds the file, not the file. |
-t, --terse |
Print all fields on one line, in a fixed order. |
Common format sequences for files:
| Sequence | Meaning |
|---|---|
%n |
File name |
%N |
Quoted name; for a symlink also -> target |
%s |
Size in bytes |
%b |
Number of allocated blocks (of %B bytes each) |
%F |
File type in words (regular file, directory, symbolic link ...) |
%a / %A |
Permissions in octal / in rwx form |
%u %U / %g %G |
Owner and group, numeric / name |
%h |
Number of hard links |
%i |
Inode number |
%x %y %z |
Access, modify, change time (human readable); %X %Y %Z give seconds since the epoch |
%w / %W |
Birth time (human readable / epoch); - or 0 when unknown |
Returns: 0 if every file could be examined; 1 if any failed (for example "No such file or directory").
Usage notes
- No
-Lby default:statdescribes the symbolic link itself, not what it points to. Add-Lfor the target. - Three timestamps, not two:
Modifychanges when the content changes,Changewhen the content or the metadata (permissions, owner, link count) changes, andAccesswhen the file is read (many systems use relatime and update it rarely). - Epoch times for arithmetic:
stat -c %Y filegives seconds since 1970, ideal for "older than N seconds" checks with$(( $(date +%s) - $(stat -c %Y f) )). - Always quote the format with single quotes so the shell does not touch the
%signs. %sis the apparent size, while%btimes%Bis the space actually allocated. A sparse file can show a large%sand a tiny allocation.- Portability: the options above are GNU
stat. BSD and macOS usestat -f FORMATwith different sequences, so scripts that must run on both need a fallback. - Birth time needs a file system and kernel that record it; otherwise
%wprints-. - For many files, one
statcall with many arguments is much faster than a loop that starts a process per file.