目录
内容来源:
GUN : Coreutils - GNU core utilities
busybox v1.36.1 : 【busybox记录】【shell指令】基于的Busybox的版本和下载方式-CSDN博客
【GUN】【stat】指令介绍
stat:报告文件或文件系统状态
stat 显示指定文件的相关信息。
简介:
stat [option]... [file]...
如果没有选项,stat将报告关于给定文件的所有信息。但是它也可以用来报告给定文件所在的文件系统的信息。如果文件是链接,stat还可以提供有关链接指向的文件的信息。
由于 shell 别名和内置的 stat 函数,以交互方式或在脚本中使用未修饰的 stat 可能会获得与此处描述的功能不同的功能。通过env(即env stat…)调用它,以避免来自shell的干扰。
‘-L’
‘--dereference’
改变stat处理符号链接的方式。使用此选项,stat将作用于每个符号链接参数所引用的文件。没有它,stat直接作用于任何符号链接参数。
‘-f’
‘--file-system’
报告关于给定文件所在的文件系统的信息,而不是关于文件本身的信息。该选项暗示了-L选项。
‘--cached=mode’
控制如何从文件系统读取属性;如果系统支持。这允许控制属性访问的新鲜度和效率之间的权衡,对于远程文件系统尤其有用。模式包括:
‘always’ 如果可用,总是读取已经缓存的属性。
‘never’ 始终同步最新的文件系统属性。这也会挂载自动挂载的文件。
‘default’ 将缓存行为保留给底层文件系统。
‘-c’
‘--format=format’
使用 format 而不是默认格式。Format会自动以换行符结束,因此使用两个或多个文件操作数运行如下命令会为每个操作数生成一行输出:
$ stat --format=%d:%i / /usr
2050:2
2057:2
‘--printf=format’
使用 format 而不是默认格式。类似于--format,但是解释反斜杠转义,并且不输出强制的尾随换行符。如果需要换行符,请在格式中包含' \n '。下面是如何使用--printf来打印/和/usr的设备和inode号:
$ stat --printf='%d:%i\n' / /usr
2050:2
2057:2
‘-t’
‘--terse’
以简洁的形式打印信息,便于其他程序解析。
以下命令的输出是相同的,而且--format还标识了以默认格式打印的项目(以更完整的形式)。格式字符串将在末尾包含另一个带有活动SELinux安全上下文的' %C '。
$ stat --format="%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %W %o" ...
$ stat --terse ...
在--file-system模式下的简洁输出:
$ stat -f --format="%n %i %l %t %s %S %b %f %a %c %d" ...
$ stat -f --terse ...
对于带有--format和--printf的文件,有效的格式指令是:
%a – 八进制的权限位(参见 ‘#’ 和 ‘0’ 打印标志)
%A – 符号形式的权限位(类似于ls -ld)
%b – 分配的块数量(参见' %b ')
%B – 由' %B '报告的每个块的字节大小
%C – 文件的SELinux安全上下文(如果可用)
%d – 十进制设备号(st dev)
%D – 十六进制设备号(st dev)
%Hd – 主设备编号,以十进制表示
%Ld – 十进制的小设备编号
%f – 十六进制的原始模式
%F – 文件类型
%g – 所有者的组ID
%G – 所有者的组名
%h – 硬链接数
%i – Inode号
%m – 挂载点(见下图)
%n – 文件名
%N – 带引号的文件名,如果是符号链接(见下文)
%o – 最佳I/ o传输大小提示
%s – 总大小,以字节为单位
%r – 十进制设备类型(st rdev)
%R – 十六进制设备类型(st rdev)
%Hr – 十进制表示的主要设备类型(见下文)
%Lr – 次要设备类型,十进制表示(见下文)
%t – 十六进制的主要设备类型(见下文)
%T – 十六进制的次要设备类型(见下文)
%u – 所有者的用户ID
%U – 所有者的用户名
%w – 文件出生时间,如果未知,则使用' - '
%W – 文件诞生的时间,单位为自Epoch或' 0 '以来的秒数
%x – 上次访问的时间
%X – 自Epoch以来最后一次访问的时间,单位为秒
%y – 上次数据修改的时间
%Y – 上次数据修改的时间,单位为Epoch以来的秒数
%z – 最后一次状态更改的时间
%Z – 上次状态更改的时间为自Epoch以来的秒数
‘%a’ 格式打印八进制模式,因此使用' # '和' 0 ' printf 标志来控制输出的零填充是有用的。例如,填充宽度至少为3,同时使更大的数字明确八进制,您可以使用‘%#03a’。
‘%N’ 格式可以通过环境变量QUOTING_STYLE设置。如果没有设置该环境变量,则默认值为‘shell-escape-always’。有效的引用样式有:
‘literal’
按原样输出字符串;这与--literal (-N)选项相同。
‘shell’
如果字符串包含shell元字符或会导致不明确的输出,则为shell引用字符串。引号适用于像bash这样的posix兼容的shell,但它并不总是适用于像csh这样不兼容的shell。
‘shell-always’
为shell引用字符串,即使它们通常不需要引用。
‘shell-escape’
像' shell '一样,但也使用POSIX建议的适用于大多数shell的' $ " '语法引用不可打印字符。
‘shell-escape-always’
像' shell-escape '一样,但是引用字符串,即使它们通常不需要引号。
‘c’
将字符串引用为C字符串字面量,包括周围的双引号字符;这与 --quote-name (-Q)选项相同。
‘escape’
将字符串引用为C字符串字面量,除了省略周围的双引号字符;这与 --escape (-b)选项相同。
‘clocale’
将字符串引用为C字符串字面量,除了使用适合区域设置的引号。
‘locale’
将字符串引用为C字符串字面量,除了使用适合语言环境的引号外,并在默认的C语言环境中引用 ’like this’ 而不是 "like this"。这在许多显示器上看起来更好。
‘r’, ‘R’, ‘%t’, 和 ‘%T’ 格式在stat(2)结构的strdev成员上操作,即表示设备而不是包含设备,因此仅为字符和块特殊文件定义。在某些系统或文件类型上,st rdev可以用来表示其他数量。
‘%W’, ‘%X’, ‘%Y’, 和 ‘%Z’ 格式接受带句点的精度,以指定要在小数点后打印的位数。例如,‘%.3X’ 输出访问时间戳,精度为毫秒级。如果给出句点但没有精度,stat使用9位数字,因此‘%.X’等于‘%.9X’。当丢弃多余的精度时,时间戳将向负无穷大方向截断。
zero pad:
$ stat -c '[%015Y]' /usr
[000001288929712]
space align:
$ stat -c '[%15Y]' /usr
[ 1288929712]
$ stat -c '[%-15Y]' /usr
[1288929712 ]
precision:
$ stat -c '[%.3Y]' /usr
[1288929712.114]
$ stat -c '[%.Y]' /usr
[1288929712.114951834]
‘%m’ 输出的挂载点与df的输出类似,不同之处在于:
stat 默认情况下不解引用符号链接(除非指定了-L)
stat 不会在文件系统列表中搜索指定的设备节点,而是直接对其进行操作
stat 输出绑定挂载文件的别名,而不是其备份设备的初始挂载点。可以递归地调用stat,直到输出没有变化,以获得当前的基本挂载点
当列出文件系统信息时(--file-system (-f)),你必须使用一组不同的格式指令:
%a – 非超级用户可用的空闲块
%b – 文件系统中的数据块总数
%c – 文件系统中的文件节点总数
%d – 文件系统中空闲的文件节点
%f – 文件系统中的空闲块
%i – 十六进制文件系统ID
%l – 文件名的最大长度
%n – 文件名
%s – 块大小(用于更快的传输)
%S – 基本块大小(用于块计数)
%t – 输入十六进制
%T – 人类可读形式的类型
时间戳根据TZ环境变量指定的时区规则列出,如果没有设置TZ,则按照系统默认规则列出。参见GNU C库参考手册中的“用TZ指定时区”一节。
退出状态为0表示成功,非0表示失败。
【busybox】【stat】指令介绍
[root@localhost bin]# ./stat --help
BusyBox v1.36.1 (2024-04-16 11:16:28 EDT) multi-call binary.
Usage: stat [-ltf] [-c FMT] FILE...
Display file (default) or filesystem status
-c FMT Use the specified format
-f Display filesystem status
-L Follow links
-t Terse display
FMT sequences for files:
%a Access rights in octal
%A Access rights in human readable form
%b Number of blocks allocated (see %B)
%B Size in bytes of each block reported by %b
%d Device number in decimal
%D Device number in hex
%f Raw mode in hex
%F File type
%g Group ID
%G Group name
%h Number of hard links
%i Inode number
%n File name
%N File name, with -> TARGET if symlink
%o I/O block size
%s Total size in bytes
%t Major device type in hex
%T Minor device type in hex
%u User ID
%U User name
%x Time of last access
%X Time of last access as seconds since Epoch
%y Time of last modification
%Y Time of last modification as seconds since Epoch
%z Time of last change
%Z Time of last change as seconds since Epoch
FMT sequences for file systems:
%a Free blocks available to non-superuser
%b Total data blocks
%c Total file nodes
%d Free file nodes
%f Free blocks
%i File System ID in hex
%l Maximum length of filenames
%n File name
%s Block size (for faster transfer)
%S Fundamental block size (for block counts)
%t Type in hex
%T Type in human readable form
【linux】【stat】指令介绍
[root@localhost bin]# stat --help
用法:stat [选项]... 文件...
显示文件或文件系统的状态。
必选参数对长短选项同时适用。
-L, --dereference 跟随链接
-f, --file-system 显示文件系统状态而非文件状态
--cached=MODE specify how to use cached attributes;
useful on remote file systems. See MODE below
-c --format=FORMAT use the specified FORMAT instead of the default;
output a newline after each use of FORMAT
--printf=FORMAT like --format, but interpret backslash escapes,
and do not output a mandatory trailing newline;
if you want a newline, include \n in FORMAT
-t, --terse print the information in terse form
--help 显示此帮助信息并退出
--version 显示版本信息并退出
The --cached MODE argument can be; always, never, or default.
`always` will use cached attributes if available, while
`never` will try to synchronize with the latest attributes, and
`default` will leave it up to the underlying file system.
The valid format sequences for files (without --file-system):
%a access rights in octal (note '#' and '0' printf flags)
%A access rights in human readable form
%b number of blocks allocated (see %B)
%B the size in bytes of each block reported by %b
%C SELinux security context string
%d device number in decimal
%D device number in hex
%f raw mode in hex
%F file type
%g group ID of owner
%G group name of owner
%h number of hard links
%i inode number
%m mount point
%n file name
%N quoted file name with dereference if symbolic link
%o optimal I/O transfer size hint
%s total size, in bytes
%t major device type in hex, for character/block device special files
%T minor device type in hex, for character/block device special files
%u user ID of owner
%U user name of owner
%w time of file birth, human-readable; - if unknown
%W time of file birth, seconds since Epoch; 0 if unknown
%x time of last access, human-readable
%X time of last access, seconds since Epoch
%y time of last data modification, human-readable
%Y time of last data modification, seconds since Epoch
%z time of last status change, human-readable
%Z time of last status change, seconds since Epoch
Valid format sequences for file systems:
%a free blocks available to non-superuser
%b total data blocks in file system
%c total file nodes in file system
%d free file nodes in file system
%f free blocks in file system
%i file system ID in hex
%l maximum length of filenames
%n file name
%s block size (for faster transfers)
%S fundamental block size (for block counts)
%t file system type in hex
%T file system type in human readable form
--terse is equivalent to the following FORMAT:
%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %W %o %C
--terse --file-system is equivalent to the following FORMAT:
%n %i %l %t %s %S %b %f %a %c %d
注意:您的shell 可能内置了自己的stat 程序版本,它会覆盖这里所提及的相应
版本。请查阅您的shell 文档获知它所支持的选项。
GNU coreutils 在线帮助:<https://www.gnu.org/software/coreutils/>
请向 <http://translationproject.org/team/zh_CN.html> 报告 stat 的翻译错误
完整文档请见:<https://www.gnu.org/software/coreutils/stat>
或者在本地使用:info '(coreutils) stat invocation'
使用示例:
后续更新