Linux命令-find命令之exec

    -exec  参数后面跟的是command命令,它的终止是以;为结束标志的,所以这句命令后面的分号是不可缺少的,考虑到各个系统中分号会有不同的意义,所以前面加反斜杠。
    {}   花括号代表前面find查找出来的文件名。
    使用find时,只要把想要的操作写在一个文件里,就可以用exec来配合find查找,很方便的。在有些操作系统中只允许-exec选项执行诸如l s或ls -l这样的命令。大多数用户使用这一选项是为了查找旧文件并删除它们。建议在真正执行rm命令删除文件之前,最好先用ls命令看一下,确认它们是所要删除的文件。 exec选项后面跟随着所要执行的命令或脚本,然后是一对儿{ },一个空格和一个\,最后是一个分号。为了使用exec选项,必须要同时使用print选项。如果验证一下find命令,会发现该命令只输出从当前路径起的相对路径及文件名。
-------------------------------------------------------------------------------------------
ls -l命令放在find命令的- exec选项中:
(find命令匹配到了当前目录下的所有普通文件,并在-exec选项中使用ls -l命令将它们列出。)
# find . -type f -exec ls -l {} \;
-rw-r--r-- 1 root root 758 12月 21 10:56 ./java_test/Test2.class
-rw-r--r-- 1 root root 593 12月 21 10:56 ./java_test/Test2.java
-rw-r--r-- 1 root root 94 12月 21 10:39 ./java_test/Test1.java
-rw-r--r-- 1 root root 415 12月 18 16:35 ./java_test/Test1.class
-rw-r--r-- 1 root root 14004 1月  11 15:30 ./test01/d.txt
-rw-r--r-- 1 root root 591 1月  12 10:20 ./test01/g.txt
-rw-r--r-- 1 root root 119 1月  11 11:33 ./test01/e.txt
-rw-r--r-- 1 root root 87 1月  11 11:28 ./test01/c.txt
-rw-r--r-- 1 root root 80 1月  11 14:29 ./test01/f.txt
-rw-r--r-- 1 root root 77 1月  10 15:33 ./test01/b.txt
-rw-r--r-- 1 root root 1016 1月  12 10:55 ./test01/h.txt
-rw-r--r-- 1 root root 32 1月  11 11:32 ./test01/a.txt
-rw-r--r-- 1 root root 355 1月  13 14:54 ./test01/tail.txt
-rw-r--r-- 1 root root 352 12月 21 14:36 ./js_test/a.js
----------------------------------------------------------------------------------------------
在目录中查找更改时间在2日以前的文件并删除它们,在删除之前先给出提示:
(ind命令在当前目录中查找所有文件名以.txt结尾、更改时间在2日以上的文件,并删除它们,只不过在删除之前先给出提示。 
按y键删除文件,按n键不删除。)

# ll
总用量 48
-rw-r--r-- 1 root root    32 1月  11 11:32 a.txt
-rw-r--r-- 1 root root    77 1月  10 15:33 b.txt
-rw-r--r-- 1 root root    87 1月  11 11:28 c.txt
-rw-r--r-- 1 root root 14004 1月  11 15:30 d.txt
-rw-r--r-- 1 root root   119 1月  11 11:33 e.txt
-rw-r--r-- 1 root root    80 1月  11 14:29 f.txt
-rw-r--r-- 1 root root   591 1月  12 10:20 g.txt
-rw-r--r-- 1 root root  1016 1月  12 10:55 h.txt
-rw-r--r-- 1 root root   355 1月  13 14:54 tail.txt
[root@lanzhu test01]# find . -name "*.txt" -mtime 2 -ok rm {} \;
< rm ... ./g.txt > ? n
< rm ... ./h.txt > ? n
-----------------------------------------------------------------------------------------------
-exec中使用grep命令:
(任何形式的命令都可以在-exec选项中使用。使用grep命令。
find命令首先匹配所有文件名为“ passwd*”的文件,例如passwd、passwd.old、passwd.bak,
然后执行grep命令看看在这些文件中是否存在一个root用户。)

# find /etc -name "passwd*" -exec grep "root" {} \;
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
-----------------------------------------------------------------------------------------------
查找文件移动到指定目录:
(查找test1中.txt文件移动到test01目录下)
# ll
总用量 16
-rw-r--r-- 1 root root  591 1月  12 10:20 g.txt
-rw-r--r-- 1 root root 1016 1月  12 10:55 h.txt
-rw-r--r-- 1 root root  355 1月  13 14:54 tail.txt
drwxr-xr-x 2 root root 4096 1月  15 10:31 test1
[root@lanzhu test01]# ll test1/
总用量 36
[root@lanzhu test01]# ll
总用量 4
drwxr-xr-x 2 root root 4096 1月  15 10:33 test1
[root@lanzhu test01]# cd test1
[root@lanzhu test1]# ll
总用量 48
-rw-r--r-- 1 root root    32 1月  11 11:32 a.txt
-rw-r--r-- 1 root root    77 1月  10 15:33 b.txt
-rw-r--r-- 1 root root    87 1月  11 11:28 c.txt
-rw-r--r-- 1 root root 14004 1月  11 15:30 d.txt
-rw-r--r-- 1 root root   119 1月  11 11:33 e.txt
-rw-r--r-- 1 root root    80 1月  11 14:29 f.txt
-rw-r--r-- 1 root root   591 1月  12 10:20 g.txt
-rw-r--r-- 1 root root  1016 1月  12 10:55 h.txt
-rw-r--r-- 1 root root   355 1月  13 14:54 tail.txt
[root@lanzhu test1]# ll
总用量 48
[root@lanzhu test01]# ll
总用量 40
-rw-r--r-- 1 root root    32 1月  11 11:32 a.txt
-rw-r--r-- 1 root root    77 1月  10 15:33 b.txt
-rw-r--r-- 1 root root    87 1月  11 11:28 c.txt
-rw-r--r-- 1 root root 14004 1月  11 15:30 d.txt
-rw-r--r-- 1 root root   119 1月  11 11:33 e.txt
-rw-r--r-- 1 root root    80 1月  11 14:29 f.txt
drwxr-xr-x 2 root root  4096 1月  15 11:08 test1
[root@lanzhu test01]# cd test1/
[root@lanzhu test1]# ll
总用量 12
-rw-r--r-- 1 root root  591 1月  12 10:20 g.txt
-rw-r--r-- 1 root root 1016 1月  12 10:55 h.txt
-rw-r--r-- 1 root root  355 1月  13 14:54 tail.txt
[root@lanzhu test1]# find . -name "*.txt" -exec mv {} .. \;
[root@lanzhu test1]# ll
总用量 0
[root@lanzhu test1]# cd ..
[root@lanzhu test01]# ll
总用量 52
-rw-r--r-- 1 root root    32 1月  11 11:32 a.txt
-rw-r--r-- 1 root root    77 1月  10 15:33 b.txt
-rw-r--r-- 1 root root    87 1月  11 11:28 c.txt
-rw-r--r-- 1 root root 14004 1月  11 15:30 d.txt
-rw-r--r-- 1 root root   119 1月  11 11:33 e.txt
-rw-r--r-- 1 root root    80 1月  11 14:29 f.txt
-rw-r--r-- 1 root root   591 1月  12 10:20 g.txt
-rw-r--r-- 1 root root  1016 1月  12 10:55 h.txt
-rw-r--r-- 1 root root   355 1月  13 14:54 tail.txt
drwxr-xr-x 2 root root  4096 1月  15 11:09 test1
------------------------------------------------------------------------------------------
用exec选项执行cp命令:
(查找test01目录中.txt文件,复制到test1目录下)
# ll
总用量 0
[root@lanzhu test1]# cd ..
[root@lanzhu test01]# ll
总用量 52
-rw-r--r-- 1 root root    32 1月  11 11:32 a.txt
-rw-r--r-- 1 root root    77 1月  10 15:33 b.txt
-rw-r--r-- 1 root root    87 1月  11 11:28 c.txt
-rw-r--r-- 1 root root 14004 1月  11 15:30 d.txt
-rw-r--r-- 1 root root   119 1月  11 11:33 e.txt
-rw-r--r-- 1 root root    80 1月  11 14:29 f.txt
-rw-r--r-- 1 root root   591 1月  12 10:20 g.txt
-rw-r--r-- 1 root root  1016 1月  12 10:55 h.txt
-rw-r--r-- 1 root root   355 1月  13 14:54 tail.txt
drwxr-xr-x 2 root root  4096 1月  15 11:09 test1
[root@lanzhu test01]# find . -name "*.txt" -exec cp {} test1 \;
cp: "./test1/d.txt" 与"test1/d.txt" 为同一文件
cp: "./test1/g.txt" 与"test1/g.txt" 为同一文件
cp: "./test1/e.txt" 与"test1/e.txt" 为同一文件
cp: "./test1/c.txt" 与"test1/c.txt" 为同一文件
cp: "./test1/f.txt" 与"test1/f.txt" 为同一文件
cp: "./test1/b.txt" 与"test1/b.txt" 为同一文件
[root@lanzhu test01]# cd test1
[root@lanzhu test1]# ll
总用量 48
-rw-r--r-- 1 root root    32 1月  15 11:18 a.txt
-rw-r--r-- 1 root root    77 1月  15 11:18 b.txt
-rw-r--r-- 1 root root    87 1月  15 11:18 c.txt
-rw-r--r-- 1 root root 14004 1月  15 11:18 d.txt
-rw-r--r-- 1 root root   119 1月  15 11:18 e.txt
-rw-r--r-- 1 root root    80 1月  15 11:18 f.txt
-rw-r--r-- 1 root root   591 1月  15 11:18 g.txt
-rw-r--r-- 1 root root  1016 1月  15 11:18 h.txt
-rw-r--r-- 1 root root   355 1月  15 11:18 tail.txt
----------------------------------------------------------------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值