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. MongoDB分片集群节点状态stateStr:RECOVERING解决

    1.关闭一直处于RECOVERING状态的mongodb server /opt/mongodb/mongodb-linux-x86_64-2.4.8/bin/mongo  127.0.0.1:220 ...

  2. ​Mac触控板常用的手势操作

    ​Mac触控板常用的手势操作 学习了:http://topbook.cc/archives/151   一个手指直接点击,类似Windows中鼠标左键功能,同时在苹果Safari等浏览器中,这个手势还 ...

  3. JSP中的TAG文件和TLD文件小结

    在jsp文件中,可以引用tag和tld文件. 1.对于tag文件 <%@ taglib prefix="ui" tagdir="/WEB-INF/tags" ...

  4. automake连载---关于两个文件configure.in和Makefile.am的编写

    http://blog.csdn.net/shanzhizi/article/details/30251763 automake主要通过编辑Makefile.am来控制它的行为,下面就常用的三个Mak ...

  5. Guava缓存使用

    public class GuavaCache { /** * LoadingCache当缓冲中不存在时,可自动加载 * */ private static LoadingCache<Integ ...

  6. 初始化linux环境

    初始化linux环境 1. 新建用户组 addgroup admin //假定为admin用户组 2. 新建用户 useradd -d /home/work -s /bin/bash -m work ...

  7. Linux打包解压

    tar tar命令可以用来压缩打包单文件.多个文件.单个目录.多个目录. 常用格式: 单个文件压缩打包: tar czvf my.tar file1 多个文件压缩打包: tar czvf my.tar ...

  8. angular中使用promise

    promise是一种用异步的方式处理值的方法,promise是对象,代表了一个函数最终可能的返回值或者抛出的异常,在与远程对象打交道时我们可以把他看作是远程对象的一个代理. 如果说是promise也是 ...

  9. 关联规则( Association Rules)之频繁模式树(FP-Tree)

    Frequent Pattern Tree(频繁模式树)是Jiawei Han在2004年的文章<Mining Frequent Patterns without Candidate Gener ...

  10. Python—— 性能分析入门指南

    虽然并非你编写的每个 Python 程序都要求一个严格的性能分析,但是让人放心的是,当问题发生的时候,Python 生态圈有各种各样的工具可以处理这类问题. 分析程序的性能可以归结为回答四个基本问题: ...