整理SCSP资料时遇到的命令,记录一下。
1.什么是stat?
stat命令主要用于显示文件或文件系统的详细信息。
使用方法如下:
[root@baynk ~]# stat --help
Usage: stat [OPTION]... FILE...
Display file or file system status.
-L, --dereference follow links
-Z, --context print the SELinux security context
-f, --file-system display file system status instead of file status
-c --format=FORMAT use the specified FORMAT instead of the default;
output a newline after each use of FORMAT
--printf=FORMAT like --format, but interpret backslash escapes,
and do not output 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 valid format sequences for files (without --file-system):
%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
%n File name
%N Quoted file name with dereference if symbolic link
%o I/O block size
%s Total size, in bytes
%t Major device type in hex
%T Minor device type in hex
%u User ID of owner
%U User name of owner
%x Time of last access
%X Time of last access as seconds since Epoch
%y Time of last modification
%Y Time of last modification as seconds since Epoch
%z Time of last change
%Z Time of last change as 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
%C SELinux security context string
%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 Type in hex
%T Type in human readable form
命令的语法格式如下:
-L 显示符号链接
-Z 打印SElinux安全上下文信息
-f 不显示文件本身的信息,显示文件所在文件系统的信息
-c 自定义输出格式,选项都需要用到-c来进行指定
-t 简洁模式,只显示摘要信息
2.stat使用实例
2.1 默认输出
[root@baynk ~]# stat sqli-labs-master.zip
File: `sqli-labs-master.zip'
Size: 3657281 Blocks: 7144 IO Block: 4096 regular file
Device: 802h/2050d Inode: 792137 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2017-11-07 00:35:03.574001590 +0800
Modify: 2017-11-07 00:34:55.626999257 +0800
Change: 2017-11-07 00:34:55.626999257 +0800
File指的是文件名
Size指的是文件大小,大约有3.6M
Blocks指使用的块数量为7144
IO Block指默认的块大小为4096Byte
Regular file 指文件类型,这里是常规文件
Device 指的是文件所在设备的主次编号,802h是十六进制,2050d是十进制。(8代表主编号,02代表分区,/proc/devices可以查看到主编号信息,这玩意查了好久资料,硬是没想到2050d是十进制dec,还是黄大仙告诉我的,这怎么受得了阿,尬死)
Inode 指inode节点数
Links 指链接数
Access 指的是文件权限
Uid/Gid 指的是文件的所属者和所属组
最下面的三个时间等会详细讲(重要),期间好奇算了下blocks和IO block的总和,好像不是3.6M,于是捣腾了半天才发现,blocks指的是扇区大小是512B为单位的,所以用3657281/4096=892.89,不足一个IO block进1就是893个,而1个IO block等于8个blocks,893*8=7144,和Blocks一致。
2.2 查看链接文件
[root@baynk ~]# stat sqli-labs-master.zip.sln
File: `sqli-labs-master.zip.sln' -> `sqli-labs-master.zip.hln'
Size: 24 Blocks: 0 IO Block: 4096 symbolic link
Device: 802h/2050d Inode: 792786 Links: 1
Access: (0777/lrwxrwxrwx) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2018-04-19 20:48:30.522345743 +0800
Modify: 2018-04-19 20:48:24.411379012 +0800
Change: 2018-04-19 20:48:24.411379012 +0800
从上面可以很显示的看出这是一个软链接,但不能看到原文件的具体信息,此时就需要加上-L参数,可以直接看到原文件的信息。
[root@baynk ~]# stat -L sqli-labs-master.zip.sln
File: `sqli-labs-master.zip.sln'
Size: 3657281 Blocks: 7144 IO Block: 4096 regular file
Device: 802h/2050d Inode: 792137 Links: 2
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2017-11-07 00:35:03.574001590 +0800
Modify: 2017-11-07 00:34:55.626999257 +0800
Change: 2018-04-19 20:47:41.488348232 +0800
2.3 查看文件系统信息
[root@baynk ~]# stat -f .
File: "."
ID: db03282c5d51ab5e Namelen: 255 Type: ext2/ext3
Block size: 4096 Fundamental block size: 4096
Blocks: Total: 5006815 Free: 4500386 Available: 4244386
Inodes: Total: 1281120 Free: 1223003
3.Linux文件的三个时间
Access Time:简写为atime,表示文件的访问时间。当文件内容被访问时,更新这个时间。 比如cat、more等查看文件的命令,但是像ls和stat则不会修改此时间。
Modify Time:简写为mtime,表示文件内容的修改时间,当文件的数据内容被修改时,更新这个时间。 像vim修改文件内容时会修改此时间。
Change Time:简写为ctime,表示文件的状态时间,当文件的状态被修改时,更新这个时间,例如文件的链接数,大小,权限,Blocks数。在2.2中我给文件创建一个软链接,增加了链接数,所以它的ctime就被修改过了的;
当然这三个时间在ls命令中也是可以查看的:
ls -l 默认情况下就是查看mtime;
ls -lc 查看文件的ctime;
ls -lu 查看文件的atime;
比如在应急响应中经常用到的,按修改时间排列文件和文件夹详细信息ls -ltr
[root@baynk test]# stat 1
File: `1'
Size: 2 Blocks: 8 IO Block: 4096 regular file
Device: 802h/2050d Inode: 792789 Links: 2
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2019-02-16 13:20:01.349999227 +0800
Modify: 2019-02-16 13:18:35.985997612 +0800
Change: 2019-02-16 13:23:20.033998556 +0800
[root@baynk test]# ls -lu 1
-rw-r--r-- 2 root root 2 Feb 16 13:20 1
[root@baynk test]# ls -l 1
-rw-r--r-- 2 root root 2 Feb 16 13:18 1
[root@baynk test]# ls -lc 1
-rw-r--r-- 2 root root 2 Feb 16 13:23 1
[root@baynk test]# ls -ltr
total 8
lrwxrwxrwx 1 root root 1 Feb 16 13:22 1.ln -> 1
-rw-r--r-- 2 root root 2 Feb 16 13:40 1.ln2
-rw-r--r-- 2 root root 2 Feb 16 13:40 1
在上述的测试过程中,发现反复使用cat等命令去查看文件并不会每次修改atime时间,原因是因为反复的去修改atime会增加磁盘的操作量,在某些只读系统上也是无法完成的,所以在Linux kernel2.6.20开始为mount引入了一个relatime选项,并从2.6.30 开始这一选项默认是开启的。当开启了relatime选项后,只有当atime<mtime或aimte<ctime时,才会去更新actime。
另外修改时间可以用touch命令来完成,touch -a可以更改atime,touch -m可以更改mtime,touch -t可以更改特定的时间,也能用touch -d来更改指定的时间,只是-t和-d后面接的字符串的格式不一样(具体的格式可以去查看man),甚至还可以使用touch -r file1 fiel2将file2的时间拷贝成file1的时间。
[root@baynk test]# touch -at 201902160000.00 1
[root@baynk test]# stat 1
File: `1'
Size: 2 Blocks: 8 IO Block: 4096 regular file
Device: 802h/2050d Inode: 792789 Links: 2
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2019-02-16 00:00:00.000000000 +0800
Modify: 2019-02-16 13:18:35.985997612 +0800
Change: 2019-02-16 13:56:45.392997540 +0800