干货:find+exec的风骚用法

find 是我们很常用的一个Linux命令,但是我们一般查找出来的额并不仅仅是看看而已,还会有进一步的操作,这个时候exec的作用就显现出来了。

  exec解释:

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

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

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

我们直接通过以下几个实例来讲解find与exec联合用法。

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

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

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

结果如下:

[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

猜你喜欢

转载自www.cnblogs.com/zatko/p/10872694.html