الفلاتر
طريقة البحث
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • "كلمتان" تجد العبارة بالضبط.
  • --name أو -x تجد خيارًا.
  • #كلمة تبحث داخل الأوامر فقط.
  • @grep تجد الدروس والمعامل التي تستخدم grep.
  • الفلاتر: cat:network · in:labs · level:rhcsa · ch:8 · root:yes. ضع ! قبل كلمة لاستبعادها. OR بين الكلمات تعني إحداها.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث

stat

Show all details of a file: size, blocks, inode, links, permissions, owner, SELinux context and all its times.

المستوى
لينكس اليومي
المجموعة
الملفات والمجلدات
يحتاج root
لا
الحزمة
coreutils

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

ماذا يفعل

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.

طريقة الكتابة

stat [OPTION]... FILE...

الخيارات 26

الأكثر استخدامًا

-c, --format=format
Print only these fields (see the format codes below).
--printf=format
Like -c, but read \n and \t, and no new line at the end.
-L, --dereference
Follow symbolic links.
-f, --file-system
Show the status of the file system instead of the file.
-t, --terse
Print everything on one line (for scripts).
--cached=mode
How to use cached data on network file systems: always, never, default.
--help
Show help.
--version
Show the version.

Format codes (for files)

%n
File name.
%s
Size in bytes.
%a
Permissions as numbers (like 644). With %A: like -rw-r--r--.
%U / %u
Owner name / owner number.
%G / %g
Group name / group number.
%i
Inode number.
%h
Number of hard links.
%F
File type (regular file, directory …).
%C
SELinux context.
%x %y %z %w
Access, modify, change and birth time (readable).
%X %Y %Z %W
The same times as seconds since 1970.
%b %B
Number of blocks, and the block size.
%d %D
Device number (decimal / hex).
%N
Quoted name, with the target for links.

Format codes (with -f)

%T
File system type (like xfs).
%S
Block size.
%b / %f / %a
Total / free / available blocks.
%c / %d
Total / free inodes.

أمثلة 11

$ مستخدم عادي# بصلاحية root (أو مع sudo)

  1. 1Show all details of a file

    stat /etc/passwd

    Size, inode, links, permissions (0644/-rw-r--r--), owner, SELinux context and 4 times.

  2. 2Get the permissions as a number

    stat -c '%a %n' /etc/shadow /etc/passwd
    0 /etc/shadow
    644 /etc/passwd

    Easy to compare with the expected values in a compliance check.

  3. 3Print owner and group

    stat -c '%U:%G' /var/www/html

    A wrong owner explains many "Permission denied" errors.

  4. 4See when a file really changed (incident response)

    stat -c 'modify=%y change=%z' /usr/bin/sudo

    If "change" is recent but "modify" is old, someone may have reset the time with touch.

  5. 5Count the hard links of a file

    stat -c '%h' data.txt

    More than 1 means other names point to the same data.

  6. 6Show the SELinux context

    stat -c '%C' /var/www/html/index.html
    unconfined_u:object_r:httpd_sys_content_t:s0

    Same information as ls -Z.

  7. 7File system info: type and free inodes

    stat -f /var

    Shows the type (xfs), block size, free blocks and free inodes.

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

  9. 9Age of a file in seconds (script)

    echo $(( $(date +%s) - $(stat -c %Y /var/backup/last.ok) ))

    Alert when the last backup flag file is too old.

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

  11. 11One-line output for a script

    stat -t /etc/hosts

    All fields in a fixed order on one line.

انتبه

  • "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.

نصائح للحفظ

  • stat -c '%a %U:%G %n' FILE = permissions, owner and name in one short line.

الدليل الكامل

هذه الصفحة تشرح الأمر بكلمات بسيطة. الدليل الأصلي فيه كل التفاصيل.

في الطرفية: man stat