find最普通的用法是查找文件,然后要对文件进行处理就需要用到参数-exec。

先看下manpage中介绍:

       -exec command ;
Execute command; true if 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 argu‐
ments 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. See the EXAMPLES section for examples of the use of the -exec
option. The specified command is run once for each matched file. The command is executed in
the starting directory. There are unavoidable security problems surrounding use of the
-exec action; you should use the -execdir option instead.

-exec  参数后面跟的是command命令,它的终止是以;为结束标志的,所以这句命令后面的分号是不可缺少的,考虑到各个系统中分号会有不同的意义,所以前面加反斜杠。

{}   花括号代表前面find查找出来的文件名。

使用find时,只要把想要的操作写在一个文件里,就可以用exec来配合find查找,很方便的。在有些操作系统中只允许-exec选项执行诸如l s或ls -l这样的命令,需要测试。大多数用户使用这一选项是为了查找旧文件并删除它们。建议在真正执行rm命令 删除文件之前,最好先用ls命令看一下,确认它们是所要删除的文件。 为了使用exec选项,必须要同时使用print选项。如果验证一下find命令,会发现该命令只输出从当前路径起的相对路径及文 件名。

看几个例子:

把所有普通文件复制到~/tmp/目录下
~$find . -type f -print -exec cp {} ~/tmp/ \;
复制文件内容到当前目录所有普通文件
find . -type f -print -exec cp ~/route {} \;
find . -type f -print -exec rm {} \;

参考:

每天一个linux命令(20):find命令之exec

xargs用法

find命令应用exec及xargs的更多相关文章

  1. find命令之exec和xargs

    exec: find是我们很常用的一个Linux命令,但是我们一般查找出来的并不仅仅是看看而已,还会有进一步的操作,这个时候exec的作用就显现出来了. exec解释: -exec  参数后面跟的是c ...

  2. 关于 ‘--exec’ 参数( find 命令)及介绍 ‘xargs ’命令区别(新版)

    前言: find 命令一直都是系统管理员的常用命令之一, 其参数中 "-exec" 尤其实用.而 "xargs" 命令,针对查询也有属于自己的见解.本文着重讲解 ...

  3. linux中exec和xargs命令的区别和优劣分析

    find的exec及ok命令 exec命令的格式为: exec command {} \; exec后面跟着的是操作命令,然后跟着{}表示每一个参数,然后空格,然后"\;".{}之 ...

  4. exec与xargs区别

    区别描述: 两者都是对符合条件的文件执行所给的Linux 命令,而不询问用户是否需要执行该命令. -exec:{}表示命令的参数即为所找到的文件,以:表示comman命令的结束.\是转义符,因为分号在 ...

  5. 每天一个 Linux 命令(20):find命令之exec

    find是我们很常用的一个Linux命令,但是我们一般查找出来的并不仅仅是看看而已,还会有进一步的操作,这个时候exec的作用就显现出来了. exec解释: -exec  参数后面跟的是command ...

  6. 每天一个linux命令(20):find命令之exec

    find是我们很常用的一个Linux命令,但是我们一般查找出来的并不仅仅是看看而已,还会有进一步的操作,这个时候exec的作用就显现出来了. exec解释: -exec  参数后面跟的是command ...

  7. find命令之exec

    find是我们很常用的一个Linux命令,但是我们一般查找出来的并不仅仅是看看而已,还会有进一步的操作,这个时候exec的作用就显现出来了. exec解释: -exec  参数后面跟的是command ...

  8. linux find命令之exec

    find是我们很常用的一个Linux命令,但是我们一般查找出来的并不仅仅是看看而已,还会有进一步的操作,这个时候exec的作用就显现出来了. exec解释: -exec  参数后面跟的是command ...

  9. PHP执行系统外部命令函数:exec()、passthru()、system()、shell_exec()

    php提供4种方法执行系统外部命令:exec().passthru().system(). shell_exec().在开始介绍前,先检查下php配置文件php.ini中是有禁止这是个函数.找到 di ...

随机推荐

  1. Net编程 详解DataTable用法【转】

    http://www.diybloghome.com/article/16.html DataTable表示一个与内存有关的数据表,可以使用工具栏里面的控件拖放来创建和使用,也可以在编写程序过程中根据 ...

  2. http://blog.csdn.net/congcong68/article/details/39256307

    http://blog.csdn.net/congcong68/article/details/39256307

  3. MongoDB分片集群常用操作

    下架主节点: db.adminCommand({replSetStepDown : 1, force : true}) 删除节点: rs.remove("IP:PORT") 新增节 ...

  4. IT博客汇

    做了个博客聚合网站,收集了一堆IT牛人的博客RSS订阅,每天自动更新. 一旦关注的作者发表了新的博客,第一时间就能看到. 地址在这儿,欢迎参观: http://www.wrox.cn

  5. JVM类加载机制详解(一)JVM类加载过程

    http://blog.csdn.net/zhangliangzi/article/details/51319033 http://chenzhou123520.iteye.com/blog/1597 ...

  6. idea中git从码云克隆项目到本地

    1.首先需要在操作系统上安装Git分布式管理系统 此处自行百度............. 2.在Intellij IDEA中配置Git 打开Settings(File-->Settings) - ...

  7. 数据採集器服务——Socket(今天才发现AES加解密代码跟贴的时候不一样,貌似乱码,不知什么情况)

    近期刚做的一个项目.关于 Socket TCP 通信. 需求方提供了一个 ARM 机器,及数据採集器,须要我做一个服务端与数据採集器进行交互. 目的: 数据採集器:定时将读取到的数据发送到服务端. 服 ...

  8. JBoss 系列十八:使用JGroups构建块RpcDispatcher构建群组通信应用

    内容概要 本部分说明JGroups构建块接口RpcDispatcher,具体提供一个简单示例来说明如何使用JGroups构建块RpcDispatcher构建群组通信应用. 示例描述 类似Message ...

  9. xcode arc 下使用 block警告 Capturing [an object] strongly in this block is likely to lead to a retain cycle” in ARC-enabled code

    xcode arc 下使用 block警告 Capturing [an object] strongly in this block is likely to lead to a retain cyc ...

  10. vue - webpack.prod.conf.js

    描述:webpack打包项目时的配置文件. 命令:yarn run build 或 npm run build 打包后,生成的文件在dist文件夹下 打包后,要在服务器环境下运行!!! 关于怎样运行, ...