stat - macOS
Overview
The stat
command provides detailed information about files and directories. It’s commonly used to retrieve file attributes, permissions, and usage statistics.
Syntax
stat [-f format] [-l] [-F format] [-n] [-t format] [-x format] [-c format] [-L] [-p] [-s] [-h] [-z] [--help] [--version] [--] files...
Options/Flags
- -f format: Specify a custom output format using a printf-style string.
- -l: Display output in long format (similar to
ls -l
). - -F format: Append a file type indicator to file name like
ls -F
. - -n: Do not translate numeric user and group IDs to names.
- -t format: Display file modification time in specified format (see
man strftime
). - -x format: Display file modification time in Extended Format (like
ls -@
). - -c format: Display file change time in specified format (see
man strftime
). - -L: Follow symbolic links when displaying information about files.
- -p: Prepend full path for each file.
- -s: Display file size in blocks.
- -h: Display file size in human-readable format.
- -z: Null-terminate output lines.
- –help: Display help for the command.
- –version: Display command version.
- —: End of options.
Examples
- Display basic file information:
stat file.txt
- Show long format output:
stat -l file.txt
- Retrieve file modification time in epoch time:
stat -f %m file.txt
- Display file path and size:
stat -p -h file.txt
- Get file ownership details:
stat -n file.txt
Common Issues
- Error when using
-L
with symbolic links: Ensure the file referenced by the symbolic link exists. - Numeric user and group IDs not translating: Use the
-n
option to prevent translation.
Integration
- Combine with
ls
:ls -l | stat -c %U %G %X
(Display file owner, group, and access time) - Use in scripts:
if [ $(stat -c %U file.txt) = "admin" ]; then echo "File owned by admin"; fi
(Check file ownership)
Related Commands
ls
file
df
- GNU stat