干货:find+exec的风骚用法

find 是咱们很经常使用的一个Linux命令,可是咱们通常查找出来的额并不只仅是看看而已,还会有进一步的操做,这个时候exec的做用就显现出来了。安全

  exec解释:bash

  -exec  参数后面跟的是 command 命令,它的终止是以“;”为结束标志的,因此这句命令后面的分号是不可缺乏的,考虑到各个系统中分号会有不一样的意义,因此前面加反斜杠。  ui

  {} 花括号表明前面find查找出来的文件名。spa

  使用find时,只要把想要的操做写在一个文件里,就能够用exec来配合find 查找,很方便的class

 

咱们直接经过如下几个实例来说解find与exec联合用法。test

 

实例1:使用find命令查找相关文件后,再使用ls命令将它们的详细信息列出来效率

咱们如今想把当前目录下全部的.o文件所有找出来,并用 ls -l 命令将它们列出来。实现这个需求的命令以下:搜索

find . -type f -exec ls -l {};grep

结果以下:command

[root@localhost test]# find . -type f -exec ls -l {} \;
-rw-r--r-- 1 root root 127 10-28 16:51 ./log2014.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-2.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-3.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-1.log
-rw-r--r-- 1 root root 33 10-28 16:54 ./log2013.log
-rw-r--r-- 1 root root 302108 11-03 06:19 ./log2012.log
-rw-r--r-- 1 root root 25 10-28 17:02 ./log.log
-rw-r--r-- 1 root root 37 10-28 17:07 ./log.txt
-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-2.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-3.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-1.log

 

 

实例2:在目录中查找更改时间为n天之前的文件,使用rm命令将它们删除

咱们如今想把当前目录下全部的文件所有找出来,并用rm命令将它们删除。实现这个需求的命令以下:

find . -type f -mtime +14  -exec rm {} \;

执行完这个命令后,该目录下全部的14天之前的文件都被删除。

输出:

[root@localhost test]# ll
总计 328
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 33 10-28 16:54 log2013.log
-rw-r--r-- 1 root root 127 10-28 16:51 log2014.log
lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log -> log.log
-rw-r--r-- 1 root root 25 10-28 17:02 log.log
-rw-r--r-- 1 root root 37 10-28 17:07 log.txt
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxrwxrwx 2 root root 4096 10-28 14:47 test4
[root@localhost test]# find . -type f -mtime +14 -exec rm {} \;
[root@localhost test]# ll
总计 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log -> log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 11-12 19:32 test3
drwxrwxrwx 2 root root 4096 11-12 19:32 test4

 

实例3:在目录中查找更改时间在n日之前的文件并删除它们,在删除以前先给出提示

在实例2中,咱们匹配到文件后就马上执行rm命令,这样操做有些危险,由于若是一旦误操做,有可能会引发灾难性的后果。

exec的安全模式就是为了不这个问题而产生。它会在匹配到某个文件后,在进行操做以前会先问一下你,通过你的确认它才会进行相应操做。

一样的实例2的需求,若是采用安全模式的话,命令是这样的:

find . -name "*.log" -mtime +14 -ok rm {} \;

执行结果以下:

[root@localhost test]# ll
总计 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log -> log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 11-12 19:32 test3
drwxrwxrwx 2 root root 4096 11-12 19:32 test4
[root@localhost test]# find . -name "*.log" -mtime +5 -ok rm {} \;
< rm ... ./log_link.log > ? y
< rm ... ./log2012.log > ? n
[root@localhost test]# ll
总计 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 11-12 19:32 test3
drwxrwxrwx 2 root root 4096 11-12 19:32 test4

 

实例4:-exec中使用grep命令

假如我如今有个很大型的项目(如Linux内核),我想在里面搜索一个含有某关键字的文件。咱们可使用grep命令检索全部的文件。这样作确定是能够的,但若是项目很大的话,这样太耗时了,效率过低。

咱们能够先用find命令找到因此相关文件,而后再用grep命令检索那些文件便可。由于已经使用find过滤一遍了,因此这样操做会节约不少时间,提升效率。

命令以下:

find /etc -name "passwd*" -exec grep  "root" {} \;

结果以下:

[root@localhost test]# find /etc -name "passwd*" -exec grep "root" {} \;
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash

 

实例5:查找文件移动到指定目录

这个需求就比较简单了。好比我如今想把全部的.o文件找出来,而后新他们mv到buil目录。命令以下:

find . -name "*.log" -exec mv {} .. \;

结果:

[root@localhost test]# ll
总计 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-12 22:49 test3
drwxrwxr-x 2 root root 4096 11-12 19:32 test4
[root@localhost test]# cd test3/
[root@localhost test3]# ll
总计 304
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:44 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
[root@localhost test3]# find . -name "*.log" -exec mv {} .. \;
[root@localhost test3]# ll
总计 0[root@localhost test3]# cd ..
[root@localhost test]# ll
总计 316
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:44 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-12 22:50 test3
drwxrwxr-x 2 root root 4096 11-12 19:32 test4

相关文章
相关标签/搜索