find命令查找目录和文件之exec

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

exec解释:

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

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

使用find时,只要把想要的操作写在一个文件里,就可以用exec来配合find查找,很方便的。在有些操作系统中只允许-exec选项执行诸如l s或ls -l这样的命令。大多数用户使用这一选项是为了查找旧文件并删除它们。建议在真正执行rm命令删除文件之前,最好先用ls命令看一下,确认它们是所要删除的文件。 exec选项后面跟随着所要执行的命令或脚本,然后是一对儿{ },一个空格和一个\,最后是一个分号。

 

实例1:ls -l命令放在find命令的-exec选项中 

命令:

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

[root@localhost test]#

说明: 

上面的例子中,find命令匹配到了当前目录下的所有普通文件,并在-exec选项中使用ls -l命令将它们列出。

[root@Slave2 test1]# find . -type f | ls -l
total 12
-rw-r--r-- 1 root root 43 Feb 28 23:13 a.txt
-rw-r--r-- 1 root root 10 Feb 28 22:52 test1.txt
-rwxr-xr-x 1 root root 13 Feb 28 23:13 test.txt

[root@Slave2 test1]# find . -type f -exec ls -l {} \;
-rwxr-xr-x 1 root root 13 Feb 28 23:13 ./test.txt
-rw-r--r-- 1 root root 43 Feb 28 23:13 ./a.txt
-rw-r--r-- 1 root root 10 Feb 28 22:52 ./test1.txt

find . -type f | ls -l  

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

和find . -type f | xargs ls -l  三种方式结果一样

 

在使用find命令的-exec选项处理匹配到的文件时, find命令将所有匹配到的文件一起传递给exec执行。但有些系统对能够传递给exec的命令长度有限制,这样在find命令运行几分钟之后,就会出现 溢出错误。错误信息通常是“参数列太长”或“参数列溢出”。这就是xargs命令的用处所在,特别是与find命令一起使用。

find命令把匹配到的文件传递给xargs命令,而xargs命令每次只获取一部分文件而不是全部,不像-exec选项那样。这样它可以先处理最先获取的一部分文件,然后是下一批,并如此继续下去。

 

 

实例2:在目录中查找更改时间在n日以前的文件并删除它们

命令:

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

输出:

[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

[root@localhost test]# 

说明

在shell中用任何方式删除文件之前,应当先查看相应的文件,一定要小心!当使用诸如mv或rm命令时,可以使用-exec选项的安全模式。它将在对每个匹配到的文件进行操作之前提示你。 

实例4:-exec使用grep命令

命令:

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

[root@localhost test]#

说明:

任何形式的命令都可以在-exec选项中使用。  在上面的例子中我们使用grep命令。find命令首先匹配所有文件名为“ passwd*”的文件,例如passwd、passwd.old、passwd.bak,然后执行grep命令看看在这些文件中是否存在一个root用户。

 -exec命令和使用管道来查询的结果是一样的

[root@Slave2 test1]# find /etc -name "passwd*"
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd


[root@Slave2 test1]# find /etc -name "passwd*" | grep "etc"  //管道grep是用来查找带有 etc目录的
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
[root@Slave2 test1]# find /etc -name "passwd*" -exec grep "etc" {} \;   //-exec grep是查找带有 etc 的所有文件中的行


abrt:x:173:173::/etc/abrt:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin

 

实例6:用exec选项执行cp命令  

命令:

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

输出:

[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

[root@localhost test]# find . -name "*.log" -exec cp {} test3 \;

cp: “./test3/log2014.log” 及 “test3/log2014.log” 为同一文件

cp: “./test3/log2013.log” 及 “test3/log2013.log” 为同一文件

cp: “./test3/log2012.log” 及 “test3/log2012.log” 为同一文件

[root@localhost test]# cd test3

[root@localhost test3]# ll

总计 304

-rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log

-rw-r--r-- 1 root root     61 11-12 22:54 log2013.log

-rw-r--r-- 1 root root      0 11-12 22:54 log2014.log

[root@localhost test3]#

 

-exec方式进行复制from test1 to test2

[root@Slave2 zh]# mkdir test2

[root@Slave2 zh]# ls
beautifulsoup4-4.1.0         ipython-5.3.0         pytest2.py  Python-3.4.0      test1
beautifulsoup4-4.1.0.tar.gz  ipython-5.3.0.tar.gz  pytest.py   Python-3.4.0.tgz  test2
[root@Slave2 zh]# cd test1
[root@Slave2 test1]# find . -type f
./test.txt
./a.txt
./test1.txt
[root@Slave2 test1]# find . -type f -exec cp {} ../test2 \;
[root@Slave2 test1]# cd ..
[root@Slave2 zhangwenqiang1]# cd test2/
[root@Slave2 test2]# ls
a.txt  test1.txt  test.txt

 

管道方式复制

[root@Slave2 test2]# find -type f
./test.txt
./a.txt
./test1.txt
[root@Slave2 test2]# find -type f | rm -f    //删除不成功
[root@Slave2 test2]# ls
a.txt  test1.txt  test.txt
[root@Slave2 test2]# find -type f | rm -f *    //删除成功
[root@Slave2 test2]# ls
[root@Slave2 test2]# cd ../test1
[root@Slave2 test1]# ls
a.txt  test1.txt  test.txt
[root@Slave2 test1]# find -type f
./test.txt
./a.txt
./test1.txt
[root@Slave2 test1]# find -type f | cp * ../test2
[root@Slave2 test1]# ls       
a.txt  test1.txt  test.txt
[root@Slave2 test1]# cd ../test2        //从test1 to test2复制成功
[root@Slave2 test2]# ls
a.txt  test1.txt  test.txt

 

从上面可以看出,通过-exec方式和管道方式的复制结果是一样的。

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值