exec解释:
-exec参数后面跟的是command命令,它的终止是以;为结束标志的,所以这句命令后面的分号是不可缺少的,考虑到各个系统中分号会有不同的意义,所以前面加反斜杠。
{}花括号代表前面find查找出来的文件名。
使用find时,只要把想要的操作写在一个文件里,就可以用exec来配合find查找,很方便的。在有些操作系统中只允许-exec选项执行诸如ls或ls-l这样的命令。大多数用户使用这一选项是为了查找旧文件并删除它们。建议在真正执行rm命令删除文件之前,最好先用ls命令看一下,确认它们是所要删除的文件。exec选项后面跟随着所要执行的命令或脚本,然后是一对儿{},一个空格和一个\,最后是一个分号。为了使用exec选项,必须要同时使用print选项。如果验证一下find命令,会发现该命令只输出从当前路径起的相对路径及文件名。
实例1:ls-l命令放在find命令的-exec选项中
命令:
find。-typef-execls-l{}\;
输出:
代码如下:
[root@localhosttest]#find。-typef-execls-l{}\;
-rw-r--r--1rootroot12710-2816:51。/log2014.log
-rw-r--r--1rootroot010-2814:47。/test4/log3-2.log
-rw-r--r--1rootroot010-2814:47。/test4/log3-3.log
-rw-r--r--1rootroot010-2814:47。/test4/log3-1.log
-rw-r--r--1rootroot3310-2816:54。/log2013.log
-rw-r--r--1rootroot30210811-0306:19。/log2012.log
-rw-r--r--1rootroot2510-2817:02。/log.log
-rw-r--r--1rootroot3710-2817:07。/log.txt
-rw-r--r--1rootroot010-2814:47。/test3/log3-2.log
-rw-r--r--1rootroot010-2814:47。/test3/log3-3.log
-rw-r--r--1rootroot010-2814:47。/test3/log3-1.log
[root@localhosttest]#
说明:
上面的例子中,find命令匹配到了当前目录下的所有普通文件,并在-exec选项中使用ls-l命令将它们列出。
实例2:在目录中查找更改时间在n日以前的文件并删除它们
命令:
find。-typef-mtime+14-execrm{}\;
输出:
代码如下:
[root@localhosttest]#ll
总计328
-rw-r--r--1rootroot30210811-0306:19log2012.log
-rw-r--r--1rootroot3310-2816:54log2013.log
-rw-r--r--1rootroot12710-2816:51log2014.log
lrwxrwxrwx1rootroot710-2815:18log_link.log-》log.log
-rw-r--r--1rootroot2510-2817:02log.log
-rw-r--r--1rootroot3710-2817:07log.txt
drwxr-xr-x6rootroot409610-2701:58scf
drwxrwxrwx2rootroot409610-2814:47test3
drwxrwxrwx2rootroot409610-2814:47test4
[root@localhosttest]#find。-typef-mtime+14-execrm{}\;
[root@localhosttest]#ll
总计312
-rw-r--r--1rootroot30210811-0306:19log2012.log
lrwxrwxrwx1rootroot710-2815:18log_link.log-》log.log
drwxr-xr-x6rootroot409610-2701:58scf
drwxrwxrwx2rootroot409611-1219:32test3
drwxrwxrwx2rootroot409611-1219:32test4
[root@localhosttest]#
说明:
在shell中用任何方式删除文件之前,应当先查看相应的文件,一定要小心!当使用诸如mv或rm命令时,可以使用-exec选项的安全模式。它将在对每个匹配到的文件进行操作之前提示你。
实例3:在目录中查找更改时间在n日以前的文件并删除它们,在删除之前先给出提示
命令:
find。-name*.log-mtime+5-okrm{}\;
输出:
代码如下:
[root@localhosttest]#ll
总计312
-rw-r--r--1rootroot30210811-0306:19log2012.log
lrwxrwxrwx1rootroot710-2815:18log_link.log-》log.log
drwxr-xr-x6rootroot409610-2701:58scf
drwxrwxrwx2rootroot409611-1219:32test3
drwxrwxrwx2rootroot409611-1219:32test4
[root@localhosttest]#find。-name*.log-mtime+5-okrm{}\;
《rm。。。。/log_link.log》?y
《rm。。。。/log2012.log》?n
[root@localhosttest]#ll
总计312
-rw-r--r--1rootroot30210811-0306:19log2012.log
drwxr-xr-x6rootroot409610-2701:58scf
drwxrwxrwx2rootroot409611-1219:32test3
drwxrwxrwx2rootroot409611-1219:32test4
[root@localhosttest]#
说明:
在上面的例子中,find命令在当前目录中查找所有文件名以.log结尾、更改时间在5日以上的文件,并删除它们,只不过在删除之前先给出提示。按y键删除文件,按n键不删除。
实例4:-exec中使用grep命令
命令:
find/etc-namepasswd*-execgreproot{}\;
输出:
代码如下:
[root@localhosttest]#find/etc-namepasswd*-execgreproot{}\;
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
[root@localhosttest]#
说明:
任何形式的命令都可以在-exec选项中使用。在上面的例子中我们使用grep命令。find命令首先匹配所有文件名为passwd*的文件,例如passwd、passwd.old、passwd.bak,然后执行grep命令看看在这些文件中是否存在一个root用户。
上一页123下一页共3页 实例5:查找文件移动到指定目录
命令:
find。-name*.log-execmv{}。。\;
输出:
代码如下:
[root@localhosttest]#ll
总计12drwxr-xr-x6rootroot409610-2701:58scf
drwxrwxr-x2rootroot409611-1222:49test3
drwxrwxr-x2rootroot409611-1219:32test4
[root@localhosttest]#cdtest3/
[root@localhosttest3]#ll
总计304
-rw-r--r--1rootroot30210811-0306:19log2012.log
-rw-r--r--1rootroot6111-1222:44log2013.log
-rw-r--r--1rootroot011-1222:25log2014.log
[root@localhosttest3]#find。-name*.log-execmv{}。。\;
[root@localhosttest3]#ll
总计0[root@localhosttest3]#cd。。
[root@localhosttest]#ll
总计316
-rw-r--r--1rootroot30210811-0306:19log2012.log
-rw-r--r--1rootroot6111-1222:44log2013.log
-rw-r--r--1rootroot011-1222:25log2014.log
drwxr-xr-x6rootroot409610-2701:58scf
drwxrwxr-x2rootroot409611-1222:50test3
drwxrwxr-x2rootroot409611-1219:32test4
[root@localhosttest]#
实例6:用exec选项执行cp命令
命令:
find。-name*.log-execcp{}test3\;
输出:
代码如下:
[root@localhosttest3]#ll
总计0[root@localhosttest3]#cd。。
[root@localhosttest]#ll
总计316
-rw-r--r--1rootroot30210811-0306:19log2012.log
-rw-r--r--1rootroot6111-1222:44log2013.log
-rw-r--r--1rootroot011-1222:25log2014.log
drwxr-xr-x6rootroot409610-2701:58scf
drwxrwxr-x2rootroot409611-1222:50test3
drwxrwxr-x2rootroot409611-1219:32test4
[root@localhosttest]#find。-name*.log-execcp{}test3\;
cp:。/test3/log2014.log及test3/log2014.log为同一文件
cp:。/test3/log2013.log及test3/log2013.log为同一文件
cp:。/test3/log2012.log及test3/log2012.log为同一文件
[root@localhosttest]#cdtest3
[root@localhosttest3]#ll
总计304
-rw-r--r--1rootroot30210811-1222:54log2012.log
-rw-r--r--1rootroot6111-1222:54log2013.log
-rw-r--r--1rootroot011-1222:54log2014.log
[root@localhosttest3]#
上面就是Linuxfind命令中-exec参数的用法介绍了,find命令的参数还有很多,如果你还想了解其他参数的使用,详见Linuxfind命令中-path-prune参数的作用介绍。