stat
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]
- GNU Coreutils [EN] @ Fedora Package
- GNU Coreutils [EN] @ Homebrew Formula
Documentation
- stat [EN] @ GNU Coreutils Manual
Syntax
stat [PARAMETER ...] FILE
Parameters
Format
- General
- The following output formats can be used with all version of stat:
- %i
- Inode number
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
- ↑ man 1 'stat'