每天一个 linux 命令( 20 ): find 命令之 exec
find 是我们很常用的一个 Linux 命令,但是我们一般查找出来的并不仅仅是看看而已,
还会有进一步的操作,这个时候 exec 的作用就显现出来了。
exec 解释:
-exec 参数后面跟的是 command 命令,它的终止是以 ;为结束标志的,所以这句命令后
面的分号是不可缺少的,考虑到各个系统中分号会有不同的意义,所以前面加反斜杠。
{} 花括号代表前面 find 查找出来的文件名。
使用 find 时,只要把想要的操作写在一个文件里,就可以用 exec 来配合 find 查找,很
方便的。 在有些操作系统中只允许 -exec 选项执行诸如 l s 或 ls -l 这样的命令。 大多数用户使
用这一选项是为了查找旧文件并删除它们。建议在真正执行 rm 命令删除文件之前,最好先
用 ls 命令看一下,确认它们是所要删除的文件。 exec 选项后面跟随着所要执行的命令或脚
本,然后是一对儿 { } ,一个空格和一个 ,最后是一个分号。为了使用 exec 选项,必须要同
时使用 print 选项。如果验证一下 find 命令,会发现该命令只输出从当前路径起的相对路径
及文件名。
实例 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 命令将它们列出。
实例 2:在目录中查找更改时间在 n 日以前的文件并删除它们
命令:
find . -type f -mtime +14 -exec rm {} \;
输出:
[root@localhost test] # ll
总计 328
评论0
最新资源