stat

From RaySoft

The stat utility displays information about the file pointed to by file. Read, write or execute per-missions permissions missions of the named file are not required, but all directories listed in the path name leading to the file must be searchable. If no argument is given, stat displays information about the file descriptor for standard input.[1]

Documentation

  • man 1 'stat'

Syntax

stat [PARAMETER ...] FILE

Parameters

GNU
The following parameters can be used with the GNU version of stat:
-c FORMAT, --format=FORMAT
Use the specified FORMAT instead of the default; output a newline after each use of FORMAT.
-Z, --context
Print security context information for SELinux if available.
BSD
The following parameters can be used with the BSD version of stat:
-f FORMAT
Display information using the specified FORMAT.

Format

General
The following output formats can be used with all version of stat:
%i
Inode number
GNU
The following output formats can be used with the GNU version of stat:
%a
Access rights in octal
%A
Access rights in human readable form
%F
File type
%s
Total size, in bytes
BSD
The following output formats can be used with the BSD version of stat:
%f
User defined flags for file (e.g. arch, nodump, hidden).
%z
The size of file in bytes.

Examples

Get information about ~/.bashrc (GNU version)
stat "${HOME}/.bashrc"

Output:

  File: `/home/alex/.bashrc'
  Size: 1987            Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 3217299     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/    alex)   Gid: ( 1000/   users)
Access: 2010-08-30 19:21:34.370234810 +0200
Modify: 2010-07-30 14:09:22.222715685 +0200
Change: 2010-08-20 12:32:47.783987399 +0200
Get information about ~/.bashrc in a script (GNU version only)
file="${HOME}/.bashrc"

read -r 'filePerm' 'fileType' <<<$(stat --format='8#%a %F' "${file}")

if [[ "${fileType}" == 'regular' && $((${filePerm} & 8#7177)) -ne 0 ]]; then
  chmod 0600 "${file}"
fi
Get information about ~/.profile (BSD version)
stat "${HOME}/.profile"

Output:

16777220 4451612 -rw------- 1 alex users 0 11699 "Apr 30 20:09:30 2014"
"Apr 24 20:29:15 2014" "Apr 27 11:38:30 2014" "Apr 24 20:29:15 2014"
4096 24 0 /home/alex/.profile
Test if a file is hidden (BSD version only)
file="${HOME}/.profile"

if [[ $(( $(stat -f '%f' "${file}") & 16#8000 )) -ne 0 ]]; then
  echo "${file} is hidden"
fi

References

  1. man 1 'stat'