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. MTK android 工程中如何修改照片详细信息中机型名

    每一个项目的机型名都不相同,因此拍出来的照片需要更改详细信息中的机型名. 那么,具体在哪里修改照片详细信息机型名呢 路径信息:/ALPS.JB3.TDD.MP.V2_TD_xxx/mediatek/c ...

  2. C语言 文件操作2--文件缓存的理解

    //文件缓存机制理解 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> void mai ...

  3. python 线性回归示例

    说明:此文的第一部分参考了这里 用python进行线性回归分析非常方便,有现成的库可以使用比如:numpy.linalog.lstsq例子.scipy.stats.linregress例子.panda ...

  4. html 元素 绝对位置坐标

    $(".seriesListings-itemContainer").click(function(){$(this).css("border","1 ...

  5. activiti-explorer:使用mysql导入外部bpmn文件后存在乱码的问题

    我已经采取了如下步骤,但是仍然还是乱码 1. 在mysql中创建数据库时指定使用utf8编码: CREATE DATABASE Activiti DEFAULT CHARACTER SET utf8 ...

  6. 系分过了,mark一下,就从这里开始吧

    算是重新归回吧,发现写博客还是这里人气比较旺,开源中国不行,动弹人气还可以,不过都没啥节操, 这么多年没来了,发现竟然还排名1150,不容易,继续加油.有种回娘家的赶脚

  7. [CareerCup] 14.2 Try-catch-finally Java中的异常处理

    14.2 In Java, does the finally block get executed if we insert a return statement inside the try blo ...

  8. LeetCode:Jump Game I II

    Jump Game Given an array of non-negative integers, you are initially positioned at the first index o ...

  9. Android调用系统相册和拍照的Demo

    最近我在群里看到有好几个人在交流说现在网上的一些Android调用系统相册和拍照的demo都有bug,有问题,没有一个完整的.确实是,我记得一个月前,我一同学也遇到了这样的问题,在低版本的系统中没问题 ...

  10. 【BZOJ 3143】【Hnoi2013】游走 期望+高斯消元

    如果纯模拟,就会死循环,而随着循环每个点的期望会逼近一个值,高斯消元就通过列方正组求出这个值. #include<cstdio> #include<cctype> #inclu ...