Use of Linux stat Command

  • 2021-08-28 21:47:14
  • OfStack

1. Brief introduction to commands

The stat command is used to display file or file system details. When displaying file information, it is more detailed than ls command.

2. Command format


stat [OPTION]... FILE..

3. Command options


-L, --dereference:跟随符号链接解析原文件而非符号链接;
-f, --file-system:显示文件所在文件系统信息而非文件信息;
-c,--format=FORMAT:以指定格式输出,而非默认格式;
	显示文件信息可用格式控制符如下:
	%a:以8进制显示访问权限
	%A:以可读形式显示访问权限
	%b:显示占有块数
	%B:显示每1块占有的字节数
	%C:SELinux security context string
	%d:10进制显示文件所在设备号
	%D:106进制显示文件所在设备号
	%f:106进制显示文件类型
	%F:文件类型。Linux下文件类型主要分为普通文件、目录、字符设备文件、块设备文件、符号链接文件、套接字等
	%g:文件所有者组ID
	%G:文件所有者组名称
	%h:文件硬链接数
	%i:inode号
	%m:文件所在磁盘分区挂载点,比如/data
	%n:文件名称
	%N:单引号括起来的文件名称,如果是软链接,则同时显示指向的文件名称
	%o:optimal I/O transfer size hint
	%s:实际文件大小,单位字节
	%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:所有者用户ID
	%U:所有者用户名称
	%w:文件创建时间,输出-表示无法得知
	%W:文件创建时间,输出Unix时间戳,0表示无法得知
	%x:可读形式输出最后访问时间atime
	%X:Unix时间戳输出最后访问时间atime
	%y:可读形式输出最后修改时间mtime
	%Y:Unix时间戳输出后修改时间mtime
	%z:可读形式输出最后状态改变时间ctime
	%Z:Unix时间戳输出最后状态改变时间ctime
	
	显示文件系统信息可用格式控制符有:
	%a:非超级用户可使用的自由block数
	%b:文件系统总block数
	%c:文件系统总文件节点数
	%d:可用文件节点数
	%f:可用文件block数
	%i:106进制文件系统ID
	%l:最大文件名称长度
	%n:文件名称
	%s:1个块的大小,单位字节(for faster transfers)
	%S:1个块的基本大小,单位字节(用于统计block的数量)
	%t:106进制输出文件系统类型
	%T:可读形式输出文件系统类型
--printf=FORMAT:以指定格式输出,而非默认格式。与--format作用类似,但可以解释反斜杠转义字符,比如换行符\n;
-t, --terse:简洁模式输出,只显示摘要信息;
--help:显示帮助信息;
--version:显示版本信息。

4. Common examples

(1) Display file information.


[root@TENCENT64 ~]# stat Changelog
 File:  ' Changelog'
 Size: 1598   	Blocks: 8     IO Block: 4096  regular file
Device: fd01h/64769d	Inode: 1579435   Links: 1
Access: (0644/-rw-r--r--) Uid: (  0/  root)  Gid: (  0/  root)
Access: 2018-11-06 22:39:54.110931887 +0800
Modify: 2018-11-06 22:39:54.110931887 +0800
Change: 2018-11-06 23:07:14.428548887 +0800
 Birth: -

Information interpretation:

File: 'Changelog': The file name is Changelog
Size: 1598: File size 1598 bytes
Blocks: 8: Number of blocks occupied by file
IO Block: 4096:
regular file: File type (normal file)
Device: fd01h/64769d: The device number of the file is displayed in 106 and 10, respectively
Inode: 1579435: File Node Number
Links: 1: Number of hard links
Access: (0644/-rw-r-r--): Access Permissions
Uid: Owner ID and Name
Gid: Owner User Group ID and Name
Access: Last access time
Modify: Last Modified Time
Change: Last state change time
Birth-: Unable to know file creation time. Note: Files under Linux do not store file creation time

(2) Display the file system information where the file is located.


[root@TENCENT64 /data/vas_pgg_proj/apps/penguin_game]# stat -f Makefile
 File: "Makefile"
  ID: 6f75a4f02634e23e Namelen: 255   Type: ext2/ext3
Block size: 4096    Fundamental block size: 4096
Blocks: Total: 43830967  Free: 30155578  Available: 27923259
Inodes: Total: 11162880  Free: 11077199

Information interpretation:

File: "Makefile": The file name is "Makefile";
ID: 6f75a4f02634e23e: File System ID
Namelen: 255: Maximum file name length
Type: ext2/ext3: File system type name
Block size: 4096: Block size 4096 bytes
Fundamental block size: 4096: Basic block size is 4096 bytes
Blocks: Total: 43830967 Free: 30155578 Available: 27923259:
Inodes: Total: 11162880 Free: 11077199

The above is the Linux stat command using the details, more information about Linux stat command please pay attention to other related articles on this site!


Related articles: