find . -name "*.txt" -exec rm {} \;
find . -name "*.txt" | xargs rm {}

-exec
    1.参数是一个一个传递的,传递一个参数执行一次rm
    2.文件名有空格等特殊字符也能处理
-xargs 
    1.一次将参数传给命令,可以使用-n控制参数个数
    2.处理特殊文件名需要采用如下方式:
    find . -name "*.txt" print0 |xargs -0 rm {}

实验结果如下,可以清楚看到参数传递过程

  1. [root@andes.com ~/tmp/dir]#find . -type f |xargs -t -n 2 echo
  2. echo ./data.txt ./env2.txt
  3. ./data.txt ./env2.txt
  4. echo ./env.txt ./export2.txt
  5. ./env.txt ./export2.txt
  6. echo ./s.txt ./d.txt
  7. ./s.txt ./d.txt
  8. echo ./export.txt ./set.txt
  9. ./export.txt ./set.txt
  10. echo ./fuck.txt
  11. ./fuck.txt
  12. [root@andes.com ~/tmp/dir]#find . -type f -exec echo begin {} \;
  13. begin ./data.txt
  14. begin ./env2.txt
  15. begin ./env.txt
  16. begin ./export2.txt
  17. begin ./s.txt
  18. begin ./d.txt
  19. begin ./export.txt
  20. begin ./set.txt
  21. begin ./fuck.txt
  22. [root@andes.com ~/tmp/dir]#

技巧: find -print0  与 xargs -0 的结合避免文件名有特殊字符如空格,引号等无法处理:

find . -name "*.txt" print0 |xargs -0 rm {}

find

-print True; print the full file name on the standard output, followed by a newline.   If you are piping the
              output of find into another program and there is the faintest possibility that the  files  which  you
              are  searching  for  might  contain  a  newline, then you should seriously consider using the -print0
              option instead of -print.  See the UNUSUAL FILENAMES section for information about how unusual  char-
              acters in filenames are handled.

-print0
              True;  print  the full file name on the standard output, followed by a null character (instead of the
              newline character that -print uses).  This allows file names that contain newlines or other types  of
              white space to be correctly interpreted by programs that process the find output.  This option corre-
              sponds to the -0 option of xargs.

xargs

-0     Input items are terminated by a null  character  insteadof  by
     whitespace,  and the quotes and backslash are not special (every
     character is taken literally).  Disables the end of file string,
     which  istreated  like
any other argument.  Useful when input
     items might contain white space, quote  marks,  or  backslashes.
     The  GNU  find  -print0  option produces input suitable for this
     mode.

find -exec 与xargs 区别的更多相关文章

  1. exec与xargs区别

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

  2. find只查当前目录 和 -exec和xargs区别

    1.find默认查找当前目录和子目录,通过maxdepth限制只查当前目录: find . -maxdepth 1 -type f -name "*.php" 2. find . ...

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

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

  4. -exec 与 xargs 的区别

    实地在shell里执行下如下命令就知道区别了: $ find -type f -exec echo {} \; 很明显,exec是对每个找到的文件执行一次命令.从这里可以看出exec的缺点是每处理一个 ...

  5. linux find查找并拷贝 exec xargs区别

    -exec    1.参数是一个一个传递的,传递一个参数执行一次rm    2.文件名有空格等特殊字符也能处理-xargs     1.一次将参数传给命令,可以使用-n控制参数个数    2.处理特殊 ...

  6. linux exec和xargs的区别

    -exec     1.参数是一个一个传递的,传递一个参数执行一次,效率低     2.文件名有空格等特殊字符也能处理 -xargs      1.一次将参数传给命令,可以使用-n控制参数个数     ...

  7. -exec和|xargs

    注意xargs会被空格割裂,所以遇到带有空格的文件名就不好办了,解决方法是使用-print0 例如:删除.目录下30天之前的.png文件 -type f -name rm 或者使用-exec:删除.目 ...

  8. test、exec、match区别

    test.exec.match的简单区别 1.test test 返回 Boolean,查找对应的字符串中是否存在模式. var str = "1a1b1c"; var reg = ...

  9. js中, match和exec方法的区别

    1. 来源分别为: string.match(reg) 和 RegExp.exec(str): 2. 区别 >  现有 字符串s1 和 正则对象 r1.     目标: 抽出s1中的所有电话号码 ...

随机推荐

  1. git pull fails “unable to resolve reference” “unable to update local ref”

    问题 由于有人rebase了分支,或者不知道怎么搞的.其他人拉取代码的时候,发现拉不下来. >git fetch error: cannot lock ref 'refs/remotes/ori ...

  2. Mono Touch Table应用

    , UIApplication.SharedApplication.StatusBarFrame.Height                         , UIScreen.MainScree ...

  3. 转:RNN(Recurrent Neural Networks)

    RNN(Recurrent Neural Networks)公式推导和实现 http://x-algo.cn/index.php/2016/04/25/rnn-recurrent-neural-net ...

  4. (转)最近一个项目中关于NGUI部分的总结(深度和drawCall)

    在自己最近的一个项目中,软件的界面部分使用了NGUI来进行制作.在制作过程中,遇到了一些问题,也获取了一些经验,总结下来,作为日后的积累. 1.NGUI图集的使用. 此次是第一个自己正儿八经的制作完整 ...

  5. IE DIV背景透明,点击事件不响应解决方案

    IE DIV背景透明,给DIV绑定点击事件, 当点击DIV时,不会响应已经绑定的点击事件. 解决方案是给DIV的设置以下样式. background-image: url(data:image/gif ...

  6. 一家VC支持企业的发展轨迹——了解每次融资后股权和期权的变化,以及股份是如何被稀释的【转载】

    来源:ReachVc 该文是 ReachVC 上一篇文章,是某个投资公司副总裁的独立博客,文章写得不错.如果你是一个不太了解融资的创业者,那么本文将对你很有用.通过武林外传同福客栈的例子,了解每次融资 ...

  7. Hadoop学习笔记(一)——编译安装和配置

    近期工作调动.打算补一下大数据处理的知识.可能会陆续涉及hadoop.mongodb.ddbs等. 首先Apache提供二进制的Hadoop版本号是32位的.在启动时总是有警告,所以想自己编译一遍.部 ...

  8. Oracle聚合连接字符串

    问题需求 现有一个数据表,如下: ID    CODE    NAME 1    A    张三 2    A    李四 3    B    王五 4    C    赵六 5    C    孙七 ...

  9. Swift语言精要 - 浅谈代理模式(Delegate)

    在iOS编程中,我们经常谈到代理代理,也就是delegate,那么什么是代理呢? 我们来看一下cocoa对它的描述: Delegation is Cocoa’s term for passing of ...

  10. 微信小程序 - tab选项卡(组件)

    更新日期: 2019/3/5:首次发布,默认下标“curIndex”超出红色提示 2019/3/7:增加tabName,可自定义数据标题名称(详情看示例) 支持单个/多个tab(显示/隐藏) 参数: ...