Linux 下 通过 stat 命令获取文件的属性,例如大小,最后的修改时间等等.
stat的属性使用方法 可以通过
"stat --help" 获取如下内容:
- Usage: stat [OPTION] FILE...
- Display file or filesystem status.
- -f, --filesystem display filesystem status instead of file status
- -c --format=FORMAT use the specified FORMAT instead of the default
- -L, --dereference follow links
- -Z, --context print the security context information if available
- -t, --terse print the information in terse form
- --help display this help and exit
- --version output version information and exit
- The valid format sequences for files (without --filesystem):
- %A Access rights in human readable form
- %a Access rights in octal
- %B The size in bytes of each block reported by `%b'
- %b Number of blocks allocated (see %B)
- %D Device number in hex
- %d Device number in decimal
- %F File type
- %f Raw mode in hex
- %G Group name of owner
- %g Group ID of owner
- %h Number of hard links
- %i Inode number
- %N Quoted File name with dereference if symbolic link
- %n File name
- %o IO block size
- %s Total size, in bytes
- %T Minor device type in hex
- %t Major device type in hex
- %U User name of owner
- %u User ID of owner
- %X Time of last access as seconds since Epoch
- %x Time of last access
- %Y Time of last modification as seconds since Epoch
- %y Time of last modification
- %Z Time of last change as seconds since Epoch
- %z Time of last change
- 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
- %C - Security context in SELinux
- %i File System id in hex
- %l Maximum length of filenames
- %n File name
- %s Optimal transfer block size
- %T Type in human readable form
- %t Type in hex
使用方式如下:
1. 不带参数 stat /path/to/myfile.ext 输出如下:
- File: `myfile.ext'
- Size: 1044611 Blocks: 2056 IO Block: 32768 regular file
- Device: xxx/xxx Inode: 1543149 Links: 1
- Access: (0664/-rw-rw-r--) Uid: (3005410/ xxxxx) Gid: (10001013/xxxxxx)
- Access: 2011-08-16 03:01:05.393004000 -0400
- Modify: 2011-08-16 04:09:30.714166000 -0400
- Change: 2011-08-16 04:09:30.714166000 -0400
2. 带参数 stat -c'%Z| %s' myfile.ext 输出如下:
- 1313486175|1118056
以上使用了'format'参数,此处使用了
- %Z Time of last change as seconds since Epoch
与
- %s Total size, in bytes
注意,如果使用多个格式标签,需要使用引号将其包裹起来,此处的 | 是自行添加的,可以自行添加\t \n 等等格式字符.