1.find默认查找当前目录和子目录,通过maxdepth限制只查当前目录:

find . -maxdepth 1 -type f -name "*.php"

2.

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 {}

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

[root@andes.com ~/tmp/dir]#find . -type f |xargs -t -n  echo
echo ./data.txt ./env2.txt
./data.txt ./env2.txt
echo ./env.txt ./export2.txt
./env.txt ./export2.txt
echo ./s.txt ./d.txt
./s.txt ./d.txt
echo ./export.txt ./set.txt
./export.txt ./set.txt
echo ./fuck.txt
./fuck.txt
[root@andes.com ~/tmp/dir]#find . -type f -exec echo begin {} \;
begin ./data.txt
begin ./env2.txt
begin ./env.txt
begin ./export2.txt
begin ./s.txt
begin ./d.txt
begin ./export.txt
begin ./set.txt
begin ./fuck.txt
[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 区别

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

  3. 【JavaEE】Hibernate继承映射,不用多态查询只查父表的方法

    几个月前,我在博问里面发了一个问题:http://q.cnblogs.com/q/64900/,但是一直没有找到好的答案,关闭问题以后才自己解决了,在这里分享一下. 首先我重复一下场景,博问里面举的动 ...

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

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

  5. MySQL 笔记整理(19) --为什么我只查一行的语句,也执行这么慢?

    笔记记录自林晓斌(丁奇)老师的<MySQL实战45讲> (本篇内图片均来自丁奇老师的讲解,如有侵权,请联系我删除) 19) --为什么我只查一行的语句,也执行这么慢? 需要说明一下,如果M ...

  6. [Linux] 终端设置只显示当前目录及终端美化

    1.只显示当前目录 vim ~/.bashrc 找到位置: if [ "$color_prompt" = yes ]; then PS1='${debian_chroot:+($d ...

  7. find命令之exec和xargs

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

  8. find命令应用exec及xargs

    find最普通的用法是查找文件,然后要对文件进行处理就需要用到参数-exec. 先看下manpage中介绍: -exec command ; Execute command; status is re ...

  9. -exec 与 xargs 的区别

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

随机推荐

  1. 解决SaveChanges会Hold住之前的错误的问题

    问题描述: 在一次新增操作中,由于有一个必填字段忘记写了,然后直接点击提交,运行到savechanges的地方,程序报错,提示***字段为必填字段. 然后关掉页面,重新填写一次,这次什么都填写上了,一 ...

  2. 20135223/20135234/20135229小组——亚博 Arduino智能小车实践报告

    实验名称:Arduino智能小车组装和综合测试 实验小组成员:20135223何伟钦 20135234马启扬 20135229吕松鸿 实验日期:2015.10.27—2015.11.3 实验时长:24 ...

  3. 使用mobile.changePage()时出现的问题(转)

    使用mobile.changePage()页面跳转,当跳转到目标页面时,目标页面中的初始化js如$().ready()及其他引入的js都无法执 行,重新刷新页面后才会执行.想到changePage() ...

  4. 嵌入式linux驱动开发之给linux系统添加温度传感器模块

    忙了几天,终于可以让ds18b20在自己的开发板的linux系统上跑了!虽然ds18b20不是什么新鲜玩意,但是想想知己可以给linux系统添加模块了还是有点小鸡冻呢! 虽然说现在硬件的资源非常丰富而 ...

  5. 系统安全扫描工具(appscan)的扫描类型小记

    扫描分类 不同场景需要使用不同方式的扫描类型.不能盲目的.暴力的去折腾. 自动扫描 刚开始扫描的时候适合用这种方式.有助于,理解整个网站的结构. 需要注意的是:去伪静态和业务冗余 伪静态 url结构相 ...

  6. SSRS开发的经验记录

    虽然工作经验相当的长,但是之前在SSRS上还没有象今天这样的经验.这只是工作经验的一点记录. 1. 定义DataSet 定义DataSet的时后,可以采用Text的方式.用Text的方式可以用一段比较 ...

  7. 自动化测试UI Test, Performance Test, Load Test 总结整理

    MSDN: 测试应用程序,Test apps early and often ,Improve Code Quality 推荐书: <Visual Studio 2015高级编程> < ...

  8. Hibernate-org.hibernate.QueryException: could not resolve property: code of:

    查询的时候有个属性跟表里的字段不符合,没有完全匹配上.

  9. Yii2修改默认布局

    public $layout = 'layout';//在类中定义一个变量,名为$layout的php文件 <?php echo $content; ?>

  10. Yii2的view需要链接跳转

    添加 use yii\helpers\Url; view中的连接 <?= Url::toRoute('post/index');?>//post为你的当前控制器名,index为view模版