stat命令
stat命令用于显示文件的状态信息,stat命令的输出信息比ls命令的输出信息要更详细。
语法:
stat (选项) (参数)
选项
-L --dereference 跟随链接
-f --file-system 显示文件系统状态而非文件状态
-c --format=格式 使用指定输出格式代替默认值,每用一次指定格式换一新行
--printf=格式 类似--format,但是会解释反斜杠转义符,不适用换行作输出结尾,
如果仍需要使用换行,可以在格式中加入"\n"
-t --terse 使用简洁格式输出
--help 显示此帮助信息并退出
--version 显示版本信息并退出
邮箱的文件格式序列(不适用 --file-system):
%a 八进制权限
案例
[root@timy-test-k8s01 ~]# stat alex.txt
File: ‘alex.txt’
Size: 17 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 67163302 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2022-04-22 05:25:44.924592851 -0400 #访问时间
Modify: 2022-04-22 05:25:09.372226161 -0400 #修改时间
Change: 2022-04-22 05:25:09.372226161 -0400 #更改时间
Birth: -
find命令
find命令用来指定目录下查找文件,任何位于参数之前的字符串都将被视为欲查找的目录名。如果使用该命令时,不设置任何参数,则find命令将在当前目录下查找子目录与文件,
并且将查找到的子目录和文件全部进行显示。
语法:
find 路径 -命令参数 [输出格式]
参数说明:
路径:告诉find在哪里去找你要的东西
参数 | 解释 |
pathname | 要查找的路径 |
option选项 | |
-maxdepth | <目录层级>:设置最大目录层级; |
-mindepth | <目录层级>:设置最小目录层级; |
tests模块 | |
-atime | 按照文件访问access的时间查找,单位是天 |
-ctime | 按照文件的改变change状态来查找文件,单位是天 |
-mtime | 根据文件修改modify时间查找文件【最常用】 |
-name | 按照文件名字查找,支持*?[]通配符 |
-group | 按照文件的所属组查找 |
-perm | 按照文件的权限查找 |
-size n [cwbkMG] | 按照文件的大小为n个由后缀决定的数据块 其中后缀为: b:代表512位元组的区块(如果用户没有指定后缀,则默认为b) c:代表字节数 k:表示kilo bytes(1024字节) w:字(2字节) M:兆字节(1048576字节) G:千兆字节(1073741824字节) |
-type查找某一类型的文件 | b - 块设备文件 d - 目录 c - 字符设备文件 p - 管道瘟胶囊 l - 符号链接文件 f - 普通文件 s - socket文件 |
-user | 按照文件属主来查找文件 |
-path | 配合-prune参数排除指定目录 |
Action模块 | |
-prune | 使find命令不在指定的目录查找 |
-delete | 删除找出的文件 |
-exec 或 -ok | 对匹配的文件执行相应的shell命令 |
将匹配的结果标准输出 | |
OPERATORS | |
! | 取反 |
-a -o | 取交集、并集、作用类似&&和\ |
案例
1.查找根目录下的所有.txt的文件
[root@timy-test-k8s01 /]# find / -name "*.txt"
2.从当前目录开始查找以0-9数字开头的文件
[root@timy-test-k8s01 Desktop]# find . -name "[0-9]*"
./1alex.txt
./2alex.txt
./3alex.txt
./4alex.txt
./5alex.txt
./6alex.txt
./7alex.txt
./8alex.txt
./9alex.txt
./10alex.txt3.从当前位置开始查找以0-9数字开头的文本类型的文件
[root@timy-test-k8s01 Desktop]# find . -type f -name "[0-9]*"
./1alex.txt
./2alex.txt
./3alex.txt
./4alex.txt
./5alex.txt
./6alex.txt
./7alex.txt
./8alex.txt
./9alex.txt
./10alex.txt4.从当前位置开始查找以0-9数字开头的文件夹
[root@timy-test-k8s01 Desktop]# find . -type d -name "[0-9]*"
./0test
./1test
./2test
./3test
./4test
./5test
./6test
./7test
./8test
./9test
5.从当前位置开始查找以0-9数字开头的文件,并删除。
[root@timy-test-k8s01 Desktop]# find . -type f -name "[0-9]*" -delete
find根据修改时间查找文件
#一天以内,被访问access过的文件
[root@timy-test-k8s01 Desktop]# find . -atime -1
.
./0test
./1test
./2test
./3test
./4test
./5test
./6test
./7test
./8test
./9test
#当前目录下,一天以内,内容变化的文件
[root@timy-test-k8s01 Desktop]# find . -mtime -1
.#根目录下恰好在7天内被访问过的文件
[root@timy-test-k8s01 Desktop]# find / -maxdepth 3 -type f -atime 7
时间说明:
-atime -2 搜索在1天内被访问过的文件
-atime 2 搜索恰好在2天前被访问过的文件
-atime +2 搜索超过2天内被访问的文件
xargs命令
xargs又称管道命令,构造参数等。
是给命令传递参数的一个过滤器,也是组合多个命令的一个工具它把一个数据流分割为一些足够小的块,以方便过滤器和命令进行处理。
简单的说就是把其他命令的给它的数据,传递给它后面的命令作为参数
-d 为输入指定一个定制的分割符,默认分隔符是空格
-i 用{}代替传递的数据
-I string 用string来代替传递的数据-n[数字]设置每次传递几行数据
-n 选项限制单个命令行的参数个数
-t 显示执行详情
-p n 允许的最大线程数量为n
-s [大小] 设置传递参数的最大字节数(小于131072字节)
-x 大于 -s 设置最大长度结束 xargs命令执行
-o --null项用null分隔,而不是空白,禁用引号和反斜杠处理
案例
1.多行输入变单行
[root@timy-test-k8s01 ~]# cat test.txt
1.h
3.e
4.d
5.l
2.8
8.o
7.t
9.j
11.y
10.x[root@timy-test-k8s01 ~]# xargs < test.txt
1.h 3.e 4.d 5.l 2.8 8.o 7.t 9.j 11.y 10.x
2.-n参数,限制每行输出个数
[root@timy-test-k8s01 ~]# xargs -n 2 <test.txt
1.h 3.e
4.d 5.l
2.8 8.o
7.t 9.j
11.y 10.x#每行显示两个数据
3.指定分隔符号
[root@timy-test-k8s01 ~]# echo "alex,timy,brade,jimmy"
alex,timy,brade,jimmy
[root@timy-test-k8s01 ~]# echo "alex,timy,brade,jimmy" | xargs -d ","
alex timy brade jimmy[root@timy-test-k8s01 ~]# echo "alex,timy,brade,jimmy" | xargs -d "," -n 2
alex timy
brade jimmy4.将当前的所有txt文件文件移动到Desktop中
[root@timy-test-k8s01 ~]# ls
alex.txt anaconda-ks.cfg Desktop mjj.txt test.txt
[root@timy-test-k8s01 ~]# find . -name "*.txt" | xargs -i mv {} Desktop/
mv: ‘./Desktop/ceshi1.txt’ and ‘Desktop/ceshi1.txt’ are the same file
mv: ‘./Desktop/test.txt’ and ‘Desktop/test.txt’ are the same file
mv: ‘./Desktop/cesh1.txt’ and ‘Desktop/cesh1.txt’ are the same file
[root@timy-test-k8s01 ~]# ls
anaconda-ks.cfg Desktop