Linux常用命令-stat

命令

stat

描述

display file or file system status
显示文件权限和时间信息

用法

1
stat [OPTION]... FILE...

选项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
Options:
-L 跟随链接跳转,即显示源文件的信息
-f, --file-system 显示文件所在的文件系统信息
-c, --format=FORMAT 指定格式并且显示换行符
--printf=FORMAT 指定格式默认不显示换行符,支持反斜杠转义,如换行符\n
-t 精简显示,即只显示数值不显示表名

注意文件和文件系统的格式选项有所不同,不支持的选项会显示为问号?
文件格式
%a access rights in octal
%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(1970-01-01 00:00:00 UTC); 0 if unknown
%x time of last access, human-readable
%X time of last access, seconds since Epoch
%y time of last modification, human-readable
%Y time of last modification, seconds since Epoch
%z time of last change, human-readable
%Z time of last change, seconds since Epoch

文件系统格式
%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

注意

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 显示文件信息
$ stat file
File: ‘file’
Size: 8 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 33594661 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1001/ app) Gid: ( 1001/ app)
Access: 2021-02-15 22:49:11.686761292 +0800
Modify: 2021-02-15 22:49:10.452861392 +0800
Change: 2021-02-15 22:49:10.452861392 +0800
Birth: -
$ stat dir1/
File: ‘dir1/’
Size: 98 Blocks: 0 IO Block: 4096 directory
Device: fd00h/64768d Inode: 17323713 Links: 2
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2021-02-15 20:30:16.012017488 +0800
Modify: 2021-02-15 20:30:14.391512782 +0800
Change: 2021-02-15 20:30:14.391512782 +0800
Birth: -
# -f显示文件所在的文件系统信息
$ stat -f file
File: "file"
ID: fd0000000000 Namelen: 255 Type: xfs
Block size: 4096 Fundamental block size: 4096
Blocks: Total: 4452864 Free: 3966145 Available: 3966145
Inodes: Total: 8910848 Free: 8867302
$ stat -f /run/utmp
File: "/run/utmp"
ID: 0 Namelen: 255 Type: tmpfs
Block size: 4096 Fundamental block size: 4096
Blocks: Total: 253487 Free: 251077 Available: 251077
Inodes: Total: 253487 Free: 252825
# -t精简显示为一行信息,只显示数值
$ stat -t file
file 8 8 81ff 1001 1001 fd00 33594661 1 0 0 1613400551 1613400550 1613400550 0 4096
$ stat -t -f file
file fd0000000000 255 58465342 4096 4096 4452864 3966196 3966196 8910848 8867302
# -c指定格式
$ stat -c %x file
2021-02-15 22:49:11.686761292 +0800
$ stat -c "%n,'%x'" file
file,'2021-02-15 22:49:11.686761292 +0800'
# 文件系统不支持%x格式选项,结果显示为?
$ stat -c "%n,'%x'" -f file
file,'?'

$ stat --format="%n\n%s\n%x" file
file\n8\n2021-02-15 22:49:11.686761292 +0800
# --printf支持斜杠转义符,如\n,\t
$ stat --printf="%n\n%s\n%x" file
file
8
2021-02-15 22:49:11.686761292 +0800
# 显示文件名%n,文件权限%a,文件类型%F,文件大小%s,owner%u,group%g,修改时间%y
$ stat --format="%n,%a,%F,%s,%u,%g,'%y'" file
file,644,regular file,8,1001,1001,'2021-02-15 22:49:10.452861392 +0800'
$ stat --printf="%n\n%a\n%F\n%s\n%u\n%g\n'%y'\n" file
file
644
regular file
8
1001
1001
'2021-02-15 22:49:10.452861392 +0800'