ls 命令及其许多参数提供了一些非常有用的文件信息。另一个不太为人所熟知的命令 stat 提供了一些更为有用的信息。
1 [[email protected] scripts]# man stat
2 STAT(1) User Commands STAT(1)3
4 NAME5 stat - display file or filesystem status6
7 SYNOPSIS8 stat[OPTION]... FILE...9
10 DESCRIPTION11 Display file or filesystem status.12
13 -L, --dereference14 follow links15
16 :17 STAT(1) User Commands STAT(1)18
19 NAME20 stat - display file or filesystem status21
22 SYNOPSIS23 stat[OPTION]... FILE...24
25 DESCRIPTION26 Display file or filesystem status.27
28 -L, --dereference29 follow links30
31 -Z, --context32 print the SELinux security context33
34 -f, --file-system35 display file system status instead of filestatus36
37 -c --format=FORMAT38 use the specified FORMAT instead of the default; output a newline39 after each use of FORMAT40
41 --printf=FORMAT42 like --format, but interpret backslash escapes, and donot output a43 mandatory trailing newline. If you want a newline, include \n in
44 FORMAT.45
46 -t, --terse47 print the information interse form48
49 --help display this help and exit50
51 --version52 output version information and exit53
54 The valid format sequences for files (without --file-system):55
56 %a Access rights inoctal57
58 %A Access rights inhuman readable form59
60 %b Number of blocks allocated (see %B)61
62 %B The size in bytes of each block reported by %b63
64 %C SELinux security context string
65
66 %d Device number in decimal
67
68 %D Device number inhex69
70 %f Raw mode inhex71
72 %F File type73
74 %g Group ID of owner75
76 %G Group name of owner77
78 %h Number of hard links79
80 %i Inode number81
82 %n File name83
84 %N Quoted file name with dereference ifsymbolic link85
86 %o I/O block size87
88 %s Total size, inbytes89
90 %t Major device type inhex91
92 %T Minor device type inhex93
94 %u User ID of owner95
96 %U User name of owner97
98 %x Time of lastaccess99
100 %X Time of lastaccess as seconds since Epoch101
102 %y Time of lastmodification103
104 %Y Time of lastmodification as seconds since Epoch105
106 %z Time of lastchange107
108 %Z Time of lastchange as seconds since Epoch109
110 Valid format sequences for filesystems:111
112 %a Free blocks available to non-superuser113
114 %b Total data blocks in filesystem115
116 %c Total file nodes in filesystem117
118 %d Free file nodes in filesystem119
120 %f Free blocks in filesystem121
122 %C SELinux security context string
123
124 %i File System ID inhex125
126 %l Maximum length of filenames127
128 %n File name129
130 %s Block size (forfaster transfers)131
132 %S Fundamental block size (forblock counts)133
134 %t Type inhex135
136 %T Type inhuman readable form137
138 NOTE: your shell may have its own version of stat, which usually super-
139 sedes the version described here. Please refer to your shell’s documenta-
140 tion fordetails about the options it supports.141
142 AUTHOR143 Written by Michael Meskes.144
145 REPORTING BUGS146 Report stat bugs to bug-147 GNU coreutils home page:
148 General help using GNU software:
149 Report stat translation bugs to
150
151 COPYRIGHT152 Copyright ? 2010 Free Software Foundation, Inc. License GPLv3+: GNU GPL153 version 3 or later .
154 This is free software: you are freeto change and redistribute it. There155 is NO WARRANTY, to the extent permitted by law.156
157 SEE ALSO158 stat(2)159
160 The full documentation for statis maintained as a Texinfo manual. If the161 info and statprograms are properly installed at your site, the command162
163 info coreutils ‘stat invocation‘
164
165 should give you access to the complete manual.166
167 GNU coreutils 8.4 October 2014 STAT(1)
man stat
下面演示了stat 不带参数显示的信息:
[[email protected] scripts]# stat date.txt
File: `date.txt‘
Size: 40 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 261790 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2017-01-29 10:49:25.119790781 +0800
Modify: 2017-01-29 10:49:11.961790098 +0800
Change: 2017-01-29 10:49:11.961790098 +0800
注意使用该命令获得的信息:除了通常的文件大小(也可以使用 ls -l 命令获得)以外,您还获得了该文件占用的块数。通常的 Linux 块大小为 512 字节,因此一个大小为 93,300,148 字节的文件将占用 (93300148/512=) 182226.85 个块。由于块都是完整占用,因此该文件使用了一些整数个数的块。无需猜测就可以获得确切的块数。
您还可以从以上输出中获得文件所有权的 GID 和 UID,以及权限的八进制表示形式 (6751)。如果要将文件恢复到它现在具有的相同权限,可以使用 chmod 6751 oracle,而不是显式拼写这些权限。
以上输出最有用的部分是文件访问时间戳信息。该输出显示,该文件被访问的时间是 2006-08-04 04:30:52(显示在“Access:”的旁边),即 2006 年 8 月 4 日上午 4:30:52。这是某个人开始使用数据库的时间。该文件的修改时间是 2005-11-02 11:49:47(显示在“Modify:”的旁边)。最后,“Change:”旁边的时间戳显示文件状态更改的时间。
stat 命令的修改符 -f 显示了有关文件系统(而非文件)的信息:
[[email protected] scripts]# stat -f date.txt
File: "date.txt"
ID: ff0d2532e41897c0 Namelen: 255 Type: ext2/ext3
Block size: 4096 Fundamental block size: 4096
Blocks: Total: 1851748 Free: 1286127 Available: 1190396
Inodes: Total: 479552 Free: 409340
另一个选项 -t 显示了完全相同的信息,只不过是在一行中显示的:
[[email protected] scripts]# stat -t date.txt
date.txt 40 8 81a4 0 0 803 261790 1 0 0 1485658165 1485658151 1485658151 4096
这对 shell 脚本非常有用,在 shell 脚本中可以使用一个简单的 cut 命令获得值以进行进一步处理。
应用案例:
如何取得stat /poe命令后文件的权限对应的数字内容,如-rw-r--r--为644,要求使用命令取得644或0644这样的数字。
[[email protected] scripts]# stat date.txt
File: `date.txt‘
Size: 40 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 261790 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2017-01-29 10:49:25.119790781 +0800
Modify: 2017-01-29 10:49:11.961790098 +0800
Change: 2017-01-29 10:49:11.961790098 +0800
[[email protected] scripts]# stat -c %a date.txt
644