国际访客建议访问 Primers 编程伙伴 国际版站点 > Bash 教程 > stat 以获得更好的体验。

# Bash 的 stat 命令

stat [OPTION]... FILE...

!subtitle:功能

查看文件的状态。

!subtitle:类型

可执行文件(/usr/bin/sleep),属于 coreutils

!subtitle:参数

  • OPTION 选项:

    • -L, --dereference - 查看符号链接的源文件,而非符号链接自身

    • -f, --file-system - 查看文件系统的状态

    • --cached=MODE - 指定怎样使用缓存属性(常用于远程文件系统)

    • -c, --format=FORMAT - 使用指定的格式,并且每个格式后输出一个换行符

    • --printf=FORMAT - 类似 --format=FORMAT 且会解析转移字符,但不会自动换行

    • -t, --terse - 使用简明的格式

    • --help - 显示帮助

    • --version - 显示版本

  • FILE - 文件列表

# 格式说明

!subtitle:用于文件的有效格式

格式符 含义
%a 权限位的八进制表示(注意 #0 printf 标志)
%A 权限位和文件类型的可读形式
%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 起的秒数

!subtitle:用于文件系统的有效格式

格式符 含义
%a 非超级用户可用的空闲块数
%b 文件系统中的数据块总数
%c 文件系统中的文件节点总数
%d 文件系统中空闲的文件节点数
%f 文件系统中的空闲块数
%i 文件系统 ID(十六进制)
%l 文件名的最大长度
%n 文件名
%s 块大小(用于加快传输速度)
%S 基本块大小(用于块计数)
%t 文件系统类型(十六进制)
%T 文件系统类型(可读形式)

# 示例

$ stat 1.txt        # 默认格式
  File: 1.txt
  Size: 62        	Blocks: 8          IO Block: 4096   regular file
Device: 259,2	Inode: 60559096    Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/   planc)   Gid: ( 1000/   planc)
Access: 2025-12-04 14:08:58.127960041 +0000
Modify: 2025-12-04 14:08:57.155939186 +0000
Change: 2025-12-04 14:08:57.160939293 +0000
 Birth: 2025-12-04 14:08:57.155939186 +0000
$ stat -t 1.txt     # 简明格式
1.txt 62 8 81b4 1000 1000 10302 60559096 1 0 0 1764857338 1764857337 1764857337 1764857337 4096

# 推荐阅读

# 手册

STAT(1)                          User Commands                         STAT(1)

NAME
       stat - display file or file system status

SYNOPSIS
       stat [OPTION]... FILE...

DESCRIPTION
       Display file or file system status.

       Mandatory  arguments  to  long  options are mandatory for short options
       too.

       -L, --dereference
              follow links

       -f, --file-system
              display file system status instead of file status

       --cached=MODE
              specify how to use cached attributes; useful on remote file sys‐
              tems. See MODE below

       -c  --format=FORMAT
              use the specified FORMAT instead of the default; output  a  new‐
              line after each use of FORMAT

       --printf=FORMAT
              like  --format, but interpret backslash escapes, and do not out‐
              put a mandatory trailing newline; if you want a newline, include
              \n in FORMAT

       -t, --terse
              print the information in terse form

       --help display this help and exit

       --version
              output version information and exit

       The MODE argument of --cached can be: always, never, or default.   'al‐
       ways'  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     permission bits in octal (note '#' and '0' printf flags)

       %A     permission bits and file type 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 (st_dev)

       %D     device number in hex (st_dev)

       %Hd    major device number in decimal

       %Ld    minor device number in decimal

       %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

       %r     device type in decimal (st_rdev)

       %R     device type in hex (st_rdev)

       %Hr    major device type in decimal, for character/block device special
              files

       %Lr    minor device type in decimal, for character/block device special
              files

       %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

       NOTE: your shell may have its own version of stat, which usually super‐
       sedes  the  version described here.  Please refer to your shell's docu‐
       mentation for details about the options it supports.

AUTHOR
       Written by Michael Meskes.

REPORTING BUGS
       GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
       Report any translation bugs to <https://translationproject.org/team/>

COPYRIGHT
       Copyright © 2023 Free Software Foundation, Inc.   License  GPLv3+:  GNU
       GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
       This  is  free  software:  you  are free to change and redistribute it.
       There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
       stat(2), statfs(2), statx(2)

       Full documentation <https://www.gnu.org/software/coreutils/stat>
       or available locally via: info '(coreutils) stat invocation'

GNU coreutils 9.4                 April 2024                           STAT(1)
本文 更新于: 2026-03-06 09:52:35 创建于: 2026-03-06 09:52:35