find使用xargs和exec执行效率比较

在linux上测试在使用find命令进行批量操作时,xargs和exec两种方式那个效率更高。

目的

在linux上测试在使用find命令进行批量操作时,xargs和exec两种方式那个效率更高。

实验

实验准备

批量创建一批文件,用来做测试使用

# 创建100个文件
[root@localhost a]# touch file{1..100}
[root@localhost a]# ls
file1    file13  file18  file22  file27  file31  file36  file40  file45  file5   file54  file59  file63  file68  file72  file77  file81  file86  file90  file95
file10   file14  file19  file23  file28  file32  file37  file41  file46  file50  file55  file6   file64  file69  file73  file78  file82  file87  file91  file96
file100  file15  file2   file24  file29  file33  file38  file42  file47  file51  file56  file60  file65  file7   file74  file79  file83  file88  file92  file97
file11   file16  file20  file25  file3   file34  file39  file43  file48  file52  file57  file61  file66  file70  file75  file8   file84  file89  file93  file98
file12   file17  file21  file26  file30  file35  file4   file44  file49  file53  file58  file62  file67  file71  file76  file80  file85  file9   file94  file99
# 修改前50个文件的文件属主和属组为jin
[root@localhost a]# chown jin:jin file{1..50}
[root@localhost a]# ll |head -10
total 0
-rw-r--r--. 1 jin  jin  0 Jul 19 11:02 file1
-rw-r--r--. 1 jin  jin  0 Jul 19 11:02 file10
-rw-r--r--. 1 root root 0 Jul 19 11:02 file100
-rw-r--r--. 1 jin  jin  0 Jul 19 11:02 file11
-rw-r--r--. 1 jin  jin  0 Jul 19 11:02 file12
-rw-r--r--. 1 jin  jin  0 Jul 19 11:02 file13
-rw-r--r--. 1 jin  jin  0 Jul 19 11:02 file14
-rw-r--r--. 1 jin  jin  0 Jul 19 11:02 file15
-rw-r--r--. 1 jin  jin  0 Jul 19 11:02 file16

time命令

time命令用来统计一个命令执行使用的时间

语法:time + 执行的命令

实例:

[root@localhost a]# time ls
file1    file13  file18  file22  file27  file31  file36  file40  file45  file5   file54  file59  file63  file68  file72  file77  file81  file86  file90  file95
file10   file14  file19  file23  file28  file32  file37  file41  file46  file50  file55  file6   file64  file69  file73  file78  file82  file87  file91  file96
file100  file15  file2   file24  file29  file33  file38  file42  file47  file51  file56  file60  file65  file7   file74  file79  file83  file88  file92  file97
file11   file16  file20  file25  file3   file34  file39  file43  file48  file52  file57  file61  file66  file70  file75  file8   file84  file89  file93  file98
file12   file17  file21  file26  file30  file35  file4   file44  file49  file53  file58  file62  file67  file71  file76  file80  file85  file9   file94  file99

real 0m0.006s
user 0m0.002s
sys 0m0.004s

xargs测试

#删除当前目录下文件属主为jin的文件
[root@localhost a]# time find ./ -user jin |xargs rm -rf 

real 0m0.006s
user 0m0.001s
sys 0m0.005s
[root@localhost a]# ls
file100  file53  file56  file59  file62  file65  file68  file71  file74  file77  file80  file83  file86  file89  file92  file95  file98
file51   file54  file57  file60  file63  file66  file69  file72  file75  file78  file81  file84  file87  file90  file93  file96  file99
file52   file55  file58  file61  file64  file67  file70  file73  file76  file79  file82  file85  file88  file91  file94  file97

可以看到使用管道配置xargs查找并删除文件属主为jin的文件一共用时0.006s,前50个文件已经被删除

exec测试

#创建file1...file50文件
[root@localhost a]# touch file{1..50}
[root@localhost a]# ls
file1    file13  file18  file22  file27  file31  file36  file40  file45  file5   file54  file59  file63  file68  file72  file77  file81  file86  file90  file95
file10   file14  file19  file23  file28  file32  file37  file41  file46  file50  file55  file6   file64  file69  file73  file78  file82  file87  file91  file96
file100  file15  file2   file24  file29  file33  file38  file42  file47  file51  file56  file60  file65  file7   file74  file79  file83  file88  file92  file97
file11   file16  file20  file25  file3   file34  file39  file43  file48  file52  file57  file61  file66  file70  file75  file8   file84  file89  file93  file98
file12   file17  file21  file26  file30  file35  file4   file44  file49  file53  file58  file62  file67  file71  file76  file80  file85  file9   file94  file99

#修改file1...file50这50个文件的属主和属组为jin
[root@localhost a]# chown jin:jin file{1..50}
[root@localhost a]# time find ./ -user jin |wc -l
50

real 0m0.032s
user 0m0.002s
sys 0m0.003s
#使用-exec删除匹配到到文件
[root@localhost a]#  time find /root/a -user jin  -exec rm -rf {} \;

real 0m0.057s
user 0m0.018s
sys 0m0.032s
[root@localhost a]# ls
file100  file53  file56  file59  file62  file65  file68  file71  file74  file77  file80  file83  file86  file89  file92  file95  file98
file51   file54  file57  file60  file63  file66  file69  file72  file75  file78  file81  file84  file87  file90  file93  file96  file99
file52   file55  file58  file61  file64  file67  file70  file73  file76  file79  file82  file85  file88  file91  file94  file97

可以看到使用exec查找并删除文件属主为jin的文件一共用时0.057s,前50个文件已经被删除.

结论

在使用find查找文件并准备做批量操作时,xargs的效率比exec的效率更高效

彩蛋

使用rm命令直接删除效率更高

[root@localhost a]# touch file{1..50}
[root@localhost a]# ls
file1    file13  file18  file22  file27  file31  file36  file40  file45  file5   file54  file59  file63  file68  file72  file77  file81  file86  file90  file95
file10   file14  file19  file23  file28  file32  file37  file41  file46  file50  file55  file6   file64  file69  file73  file78  file82  file87  file91  file96
file100  file15  file2   file24  file29  file33  file38  file42  file47  file51  file56  file60  file65  file7   file74  file79  file83  file88  file92  file97
file11   file16  file20  file25  file3   file34  file39  file43  file48  file52  file57  file61  file66  file70  file75  file8   file84  file89  file93  file98
file12   file17  file21  file26  file30  file35  file4   file44  file49  file53  file58  file62  file67  file71  file76  file80  file85  file9   file94  file99
[root@localhost a]# time rm -rf file{1..50}

real 0m0.003s
user 0m0.001s
sys 0m0.002s
[root@localhost a]# ls
file100  file53  file56  file59  file62  file65  file68  file71  file74  file77  file80  file83  file86  file89  file92  file95  file98
file51   file54  file57  file60  file63  file66  file69  file72  file75  file78  file81  file84  file87  file90  file93  file96  file99
file52   file55  file58  file61  file64  file67  file70  file73  file76  file79  file82  file85  file88  file91  file94  file97