find-exec命令总是报缺参数错误
前⾔:最近⼏天使⽤find的⾼级功能,但执⾏到 -exec命令的时候总是提⽰错误
信息如下:“find: missing argument to `-ok' ”,花了点时间,研究了下帮助(man),终于是搞清楚了。
说明:find命令,配合-exec参数,可以对查询的⽂件进⾏进⼀步的操作,可以得到很多有⽤的功能,⽐如说⽂件包含特定字符串的查询等,要了解这个功能,最简单直接的就是看find命令帮助,列出
-exec command ;
Execute command; true if 0 status is returned.  All  following  arguments  to find are taken to be arguments to the command until an  argument  consisting of #;' is encountered.  The string {}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of find.  Both of these constructions might need to be escaped (with a \') or quoted to  protect  them  from  expansion  by the shell.  The command is executed in the starting directory.
find查命令的使用其实只要读懂这段话就理解了
废话少说,这⾥简单说明⼀下
-exec 参数后⾯跟的是 command命令,注意点如下:
command命令的终⽌,使⽤ ';' (分号)来判定,在后⾯必须有⼀个 ';'
'{}',使⽤{}来表⽰⽂件名,也就是find前⾯处理过程中过滤出来的⽂件,⽤于command命令进⾏处理
特别强调,对于不同的系统,直接使⽤分号可能会有不同的意义, 使⽤转义符 '\'在分号前明确说明,对于前⾯我们遇到的问题,主要就是这个原因引起的!
举例:
1.查询所有保护字符串“Hello”的⽂件
find / -exec grep "Hello" {} \;
2.删除所有临时⽂件
find / -name "*.tmp" -exec rm -f {} \;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。