# 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.

Source: https://www.jpkc.com/db/en/cheatsheets/files-text/stat/

<!-- PROSE:intro -->
`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.
<!-- PROSE:intro:end -->

## Basic Usage

`stat <file>` — Show all available information about a file.

```bash
stat index.html
```

`stat -f <filesystem>` — Display filesystem status instead of file status.

```bash
stat -f /
```

`stat -L <symlink>` — Follow symlinks and show info about the target file.

```bash
stat -L /usr/bin/python3
```

`stat <file1> <file2>` — Show information for multiple files.

```bash
stat *.conf
```

## Format Strings — File Info

`stat -c '%s' <file>` — Show file size in bytes.

```bash
stat -c '%s' backup.tar.gz
```

`stat -c '%n %s' <file>` — Show filename and size.

```bash
stat -c '%n %s' *.log
```

`stat -c '%A' <file>` — Show human-readable permissions (e.g., -rwxr-xr-x).

```bash
stat -c '%A' script.sh
```

`stat -c '%a' <file>` — Show permissions as octal number (e.g., 755).

```bash
stat -c '%a' /etc/passwd
```

`stat -c '%U:%G' <file>` — Show owner and group names.

```bash
stat -c '%U:%G' /var/log/syslog
```

`stat -c '%F' <file>` — Show file type (regular file, directory, symlink, etc.).

```bash
stat -c '%F' /dev/null
```

`stat -c '%i' <file>` — Show inode number.

```bash
stat -c '%i' myfile.txt
```

## Format Strings — Timestamps

`stat -c '%y' <file>` — Show last modification time (human-readable).

```bash
stat -c '%y' config.yaml
```

`stat -c '%x' <file>` — Show last access time.

```bash
stat -c '%x' data.csv
```

`stat -c '%z' <file>` — Show last status change time (ctime).

```bash
stat -c '%z' /etc/hosts
```

`stat -c '%w' <file>` — Show file creation (birth) time if available.

```bash
stat -c '%w' readme.md
```

`stat -c '%Y' <file>` — Show modification time as Unix timestamp (seconds since epoch).

```bash
stat -c '%Y' deploy.log
```

## Filesystem Info

`stat -f -c '%T' <path>` — Show filesystem type.

```bash
stat -f -c '%T' /
```

`stat -f -c '%b %S' <path>` — Show total blocks and block size.

```bash
stat -f -c '%b %S' /home
```

`stat -f -c '%a' <path>` — Show available blocks for unprivileged users.

```bash
stat -f -c '%a' /tmp
```

## Common Patterns

`stat -c '%a %n' *` — List permissions of all files in current directory.

```bash
stat -c '%a %n' /etc/*.conf
```

`stat -c '%s %n' * | sort -n` — List files sorted by size.

```bash
stat -c '%s %n' *.log | sort -n
```

`stat -c '%Y %n' * | sort -n | tail -5` — Show 5 most recently modified files.

```bash
stat -c '%Y %n' /var/log/* | sort -n | tail -5
```

`stat -c '%U %n' * | grep -v root` — Find files not owned by root.

```bash
stat -c '%U %n' /usr/local/bin/* | grep -v root
```

<!-- PROSE:outro -->
## 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](https://www.gnu.org/software/coreutils/manual/html_node/stat-invocation.html) – official reference for all format strings
- [Wikipedia: stat (system call)](https://en.wikipedia.org/wiki/Stat_(system_call)) – background on the underlying file-status system call
<!-- PROSE:outro:end -->

## Related Commands

- [ls](https://www.jpkc.com/db/en/cheatsheets/files-text/ls/) – directory contents with permissions and size at a glance
- [file](https://www.jpkc.com/db/en/cheatsheets/files-text/file/) – determine the file type by content
- [chmod](https://www.jpkc.com/db/en/cheatsheets/files-text/chmod/) – change access permissions on files

