Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
Wat het doet
stat prints the inode information of a file. It shows more than ls -l: the permissions as numbers (0644), the inode number, the number of hard links, the device, and four times:
- Access: last read.
- Modify: last change of the data.
- Change: last change of the inode (permissions, owner, links). It cannot be set with
touch. - Birth: when the file was created (if the file system knows it).
With -c FORMAT you choose exactly what to print, which is great in scripts.
Zo schrijf je het
stat [OPTION]... FILE...Opties 26
Meest gebruikt
Format codes (for files)
Format codes (with -f)
Niets past bij dit filter.
Voorbeelden 11
$ als gewone gebruiker# als root (of met sudo)
1Show all details of a file
stat /etc/passwdSize, inode, links, permissions (0644/-rw-r--r--), owner, SELinux context and 4 times.
2Get the permissions as a number
stat -c '%a %n' /etc/shadow /etc/passwd 0 /etc/shadow 644 /etc/passwdEasy to compare with the expected values in a compliance check.
3Print owner and group
stat -c '%U:%G' /var/www/htmlA wrong owner explains many "Permission denied" errors.
4See when a file really changed (incident response)
stat -c 'modify=%y change=%z' /usr/bin/sudoIf "change" is recent but "modify" is old, someone may have reset the time with touch.
5Count the hard links of a file
stat -c '%h' data.txtMore than 1 means other names point to the same data.
6Show the SELinux context
stat -c '%C' /var/www/html/index.html unconfined_u:object_r:httpd_sys_content_t:s0Same information as
ls -Z.7File system info: type and free inodes
stat -f /varShows the type (xfs), block size, free blocks and free inodes.
8Show the target of a link
stat -c '%N' /etc/localtime '/etc/localtime' -> '../usr/share/zoneinfo/Asia/Riyadh'Here: the time zone of the system.
9Age of a file in seconds (script)
Alert when the last backup flag file is too old.
10Compare permissions of many files
stat -c '%a %U:%G %n' /etc/ssh/*The private host keys must be 600 (or 640 for group ssh_keys), owned by root.
11One-line output for a script
stat -t /etc/hostsAll fields in a fixed order on one line.
Niets past bij dit filter.
Let op
- "Change" (ctime) is not the creation time; it is the last change of the file's metadata.
- Birth time can show "-" when the file system or kernel does not provide it.
Tips om te onthouden
stat -c '%a %U:%G %n' FILE= permissions, owner and name in one short line.