[关闭]
@ghimi 2018-05-24T14:59:25.000000Z 字数 1063 阅读 1560

find: missing argument to '-ok'

linux find


错误信息如下:"find:missing argument to '-ok'"
说明: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.

其实只要读懂这段话就理解了
-exec 参数后面跟的是 command命令,注意点如下:
command命令的终止,使用 ';' (分号)来判定,在后面必须有一个 ';'
'{}',使用{}来表示文件名,也就是find前面处理过程中过滤出来的文件,用于command命令进行处理
特别强调,对于不同的系统,直接使用分号可能会有不同的意义, 使用转义符 '\'在分号前明确说明,对于前面我们遇到的问题,主要就是这个原因引起的!
举例:
* 查询所有保护字符串“Hello”的文件

  1. find / -exec grep "Hello" {} \;
  1. find / -name "*.tmp" -exec rm -f {} \;

find查找当前目录

  1. # -maxdepth 参数是指定当前查找的深度的,当指定为1时为查找当前目录下对应类型的文件
  2. find . -maxdepth 1 -type f

find 统计当前目录下有多少文件

  1. # 管道前面的命令每一行输出一个文件,之后统计行数就获得了当前目录下的文件数
  2. find . -maxdepth 1 -type f | wc -l
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注