一 exec 和args 结合find命令区别
find 和 exec 、 xargs 搭配使用也是有区别的:
1、exec
find将匹配到的文件传给exec执行,每找到一次传一次,但是如果文件名很多,导致命令太长,会导致执行失败。
2、xargs
解决一些命令不支持从管道传来的标准输入的参数,xargs作用将标准输入转成命令行参数( xargs [-options] [command]
),简单小例子:
# echo不支持从管道输入参数
[root@iZwz90jb8mqajkli0ttrcbZ ~]# echo "abc" | echo
# 加上args就可以
[root@iZwz90jb8mqajkli0ttrcbZ ~]# echo "abc" |xargs echo
abc
二 常用查找
# 查找当前目录普通文件类型,通过file命令查更详细信息
find ./ -type f -print | xargs file
# 查找指定用户abc的文件
find ./ -user abc
# 查找修改时间在5天以上(不包括5天),且小于10天的文件
find / -mtime +5 -mtime -10
#查找过去一个小时修改的文件
find / -cmin -60
#查找大于500MB 小于1000MB的文件
find / -size 500M -size -1000M
-mtime指定的参数为天,有三种形式-n, +n 和n 含义如下图
n含义图
说明:
最右边为当前时,+5 代表大于等于 6 天前的档案名, -5 代表小于等于 5 天内的档案名,5 则是代表 5-6 那一天的档案名。
三 批量收回相应的写权限
find . -perm -7 -print | xargs chmod o-w
四 在当前目录搜索特定字符hello
# 如果只有一层可以简单的:
grep hello *
# 如果多层要搜索
find ./ \* |xargs grep hello
五 批量改名
批量改名可以用rename或find结合xargs
[root@iZwz90jb8mqajkli0ttrcbZ test]# ls -al
总用量 0
drwxr-xr-x 2 root root 45 12月 1 21:12 .
drwxr-xr-x 5 root root 85 12月 1 21:12 ..
-rw-r--r-- 1 root root 0 12月 1 21:12 1.log
-rw-r--r-- 1 root root 0 12月 1 21:12 2.log
-rw-r--r-- 1 root root 0 12月 1 21:12 3.log
[root@iZwz90jb8mqajkli0ttrcbZ test]# man rename
[root@iZwz90jb8mqajkli0ttrcbZ test]# rename .log .deal *.log
[root@iZwz90jb8mqajkli0ttrcbZ test]# ll
总用量 0
-rw-r--r-- 1 root root 0 12月 1 21:12 1.deal
-rw-r--r-- 1 root root 0 12月 1 21:12 2.deal
-rw-r--r-- 1 root root 0 12月 1 21:12 3.deal
[root@iZwz90jb8mqajkli0ttrcbZ test]# echo "1.log 2.log 3.log" |xargs touch
[root@iZwz90jb8mqajkli0ttrcbZ test]# ls
1.log 2.log 3.log
[root@iZwz90jb8mqajkli0ttrcbZ test]# find ./ -name "*log" | xargs rename .log .deal
[root@iZwz90jb8mqajkli0ttrcbZ test]# ll
总用量 0
-rw-r--r-- 1 root root 0 12月 1 21:22 1.deal
-rw-r--r-- 1 root root 0 12月 1 21:22 2.deal
-rw-r--r-- 1 root root 0 12月 1 21:22 3.deal
六 限制参数行数
xargs 将通过管道过来的标准输入作为参数,但是如果我们的参数很长,比如我们需要删除满足条件的日志文件,如果文件很长,都拼在一起,执行报错,会得到命令太长的错误。
如:
# 创建10000个空文件
echo {0..10000} |xargs touch
# 看到了嘛,将所有的输入都变成了file的参数,如果文件非常多就会报参数过多
# -t 选项在命令执行前打印它
[root@iZwz90jb8mqajkli0ttrcbZ test]# find ./ -type f |xargs -t file|more
file ./55 ./56 ./57 ./58 ./59 ./60 ./61 ./62 ./63 ./64 ./65 ./66 ./0 ./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 ./67 ./68 ./69 ./70 ./71 ./72 ./73 ./74 ./75 ./76 ./77 ./78 ./79 ./80 ./81 ./82 ./83 ./84 ./85 ./86 ./87 ./88 ./89 ./90 ./91 ./92 ./93 ./94 ./95 ./96 ./97 ./98 ./99 ./100 ./101 ./102 ./103 ./104 ./105 ./106 ./107 ./108 ./109 ./110 ./111 ./112
...
# 对于长参数情况,我们可以通过-ln通过n来限制一次执行多少行作为命令的参数
# -n2 限制输入的命令参数
[root@iZwz90jb8mqajkli0ttrcbZ test]# find ./ -type f |xargs -l2 -t file
file ./4106 ./4107
./4106: empty
./4107: empty
file ./4108 ./4109
....
理解了嘛,这个参数非常实用。
八 多进程执行
xargs的命令必须一个个执行,我们可以通过--max-procs 或-P来指定同时运行的进程:
[root@iZwz90jb8mqajkli0ttrcbZ test]# time find ./ -type f |xargs -P 2 -n2 -t file
file ./5098 ./5099
file ./5100 ./5101
./5098: empty
./5099: empty
./5100: empty
./5101: empty
^C
real 0m3.924s
user 0m1.932s
sys 0m3.559s
[root@iZwz90jb8mqajkli0ttrcbZ test]# time find ./ -type f |xargs -n2 -t file
...
file ./9996 ./9997
./9996: empty
./9997: empty
file ./9998 ./9999
./9998: empty
./9999: empty
file ./10000
./10000: empty
real 0m5.455s
user 0m2.080s
sys 0m3.471s
明显看出通过-P指定了最多两个进程比默认的1个进程要快。
九 删除带空格或换行的文件
因为args默认是按照空格和换行分隔的,如果文件名本身含有换行或空格符号就无法删除,可以通过下面特殊命令删除。
[root@iZwz90jb8mqajkli0ttrcbZ test]# time find ./ -type f |xargs rm
rm: 无法删除'./abc': 没有那个文件或目录
rm: 无法删除'aaa': 没有那个文件或目录
real 0m0.189s
user 0m0.023s
sys 0m0.165s
[root@iZwz90jb8mqajkli0ttrcbZ test]# ls
'abc aaa'
[root@iZwz90jb8mqajkli0ttrcbZ test]# find ./ -type f -print0|xargs -0 rm
[root@iZwz90jb8mqajkli0ttrcbZ test]# ls
[root@iZwz90jb8mqajkli0ttrcbZ test]#
网友评论