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. ArrayList源码深度解析

    jdk:1.8 一.先看看ArrayList类的整体概述, ArraList是基于动态数组实现的一种线性列表,这种基于动态数组的好处就是索引比较快,时间复杂度为O(1):但是对数据修改比较慢,因为需要 ...

  2. stderr重定向

    在测试alljoyn时开启了内部日志,输出太多想重定向到文件中,使用如下命令: ./chat -s aaa >.txt 居然还是打印到屏幕上而不是输出到文件中. 查看alljoyn写日志的代码, ...

  3. 利用内存分析工具(Memory Analyzer Tool,MAT)分析java项目内存泄露

    转载:http://blog.csdn.net/wanghuiqi2008/article/details/50724676 一.开发环境: 操作系统:ubuntu 14.04 IDE:Eclipse ...

  4. 元素的数据存储-jQuery.data()与.data()

    jQuery提供的存储接口 jQuery.data( element, key, value ) //静态接口,存数据 jQuery.data( element, key ) //静态接口,取数据 . ...

  5. lodash 移除数据元素 pull without 删除数组元素

    _.pull(array, [values]) 移除所有经过 SameValueZero 等值比较为 true 的元素 . without 不会修改原数组 <!DOCTYPE html> ...

  6. js设置百分比保留两位小数

      CreateTime--2017年8月23日11:03:31Author:Marydon js设置百分比保留两位小数 错误用法: var percent = (num1/num2) * 100%; ...

  7. Windows 10 KMS 激活方法

    本篇文章由:http://xinpure.com/windows-10-activate-method/ 摘抄: http://www.nruan.com/win-key.html 须知:如果需要在线 ...

  8. iTunes 无法添加 iPhone 自定义铃声

    本篇文章由:http://xinpure.com/itunes-unable-to-add-iphone-custom-ringtones/ 新版本 iTunes 需要在 菜单栏 -> 文件 中 ...

  9. 前端资源管理工具sourcetree

    https://www.sourcetreeapp.com/

  10. 使用css3属性transition实现页面滚动

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content ...