command: find命令-exec参数的使用说明(转)

来源:互联网 发布:手机贴膜有必要吗 知乎 编辑:程序博客网 时间:2024/05/16 08:16
find命令,配合-exec参数,可以对查询的文件进行进一步的操作,可以得到很多有用的功能,比如说文件包含特定字符串的查询等,要了解这个功能,最 简单直接的就是看find命令帮助,列出find命令,配合-exec参数,可以对查询的文件进行进一步的操作,可以得到很多有用的功能,比如说文件包含 特定字符串的查询等,要了解这个功能,最简单直接的就是看find命令帮助,man 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命令进行处理

特别强调,对于不同的系统,直接使用分号可能会有不同的意义, 使用转义符 '/'在分号前明确说明。

举例:

1.查询所有包含字符串“Hello”的文件

find / -exec grep "Hello" {} /;

2.删除所有临时文件

find / -name "*.tmp" -exec rm -f {} /;


另外,上面这两条命令都可以用xargs来实现,比如:

1.find / | xargs grep "Hello"

2.find / -name "*.tmp"  | xargs rm -f

原创粉丝点击