默认情况下, find 每输出一个文件名, 后面都会接着输出一个换行符 ('\n'), 因此我们看到的 find 的输出都是一行一行的:

[bash-4.1.5] ; ls -l

total 0

-rw-r--r-- 1 root root 0 2010-08-02 18:09 file1.log

-rw-r--r-- 1 root root 0 2010-08-02 18:09 file2.log

[bash-4.1.5] ; find -name '*.log'

./file2.log

./file1.log

比如我想把所有的 .log 文件删掉, 可以这样配合 xargs 一起用:

[bash-4.1.5] ; find -name '*.log'

./file2.log

./file1.log

[bash-4.1.5] ; find -name '*.log' | xargs rm

[bash-4.1.5] ; find -name '*.log'

嗯, 不错, find+xargs 真的很强大. 然而:

[bash-4.1.5] ; ls -l

total 0

-rw-r--r-- 1 root root 0 2010-08-02 18:12 file 1.log

-rw-r--r-- 1 root root 0 2010-08-02 18:12 file 2.log

[bash-4.1.5] ; find -name '*.log'

./file 1.log

./file 2.log

[bash-4.1.5] ; find -name '*.log' | xargs rm

rm: cannot remove `./file': No such file or directory

rm: cannot remove `1.log': No such file or directory

rm: cannot remove `./file': No such file or directory

rm: cannot remove `2.log': No such file or directory

原因其实很简单, xargs 默认是以空白字符 (空格, TAB, 换行符) 来分割记录的, 因此文件名 ./file 1.log 被解释成了两个记录 ./file 和 1.log, 不幸的是 rm 找不到这两个文件.

为了解决此类问题, 聪明的人想出了一个办法, 让 find 在打印出一个文件名之后接着输出一个 NULL 字符 ('\0') 而不是换行符, 然后再告诉 xargs 也用 NULL 字符来作为记录的分隔符. 这就是 find 的 -print0 和 xargs 的 -0 的来历吧.

[bash-4.1.5] ; ls -l

total 0

-rw-r--r-- 1 root root 0 2010-08-02 18:12 file 1.log

-rw-r--r-- 1 root root 0 2010-08-02 18:12 file 2.log

[bash-4.1.5] ; find -name '*.log' -print0 | hd

0  1  2  3   4  5  6  7   8  9  A  B   C  D  E  F  |0123456789ABCDEF|

--------+--+--+--+--+---+--+--+--+---+--+--+--+---+--+--+--+--+----------------|

00000000: 2e 2f 66 69  6c 65 20 31  2e 6c 6f 67  00 2e 2f 66  |./file 1.log../f|

00000010: 69 6c 65 20  32 2e 6c 6f  67 00                     |ile 2.log.      |

[bash-4.1.5] ; find -name '*.log' -print0 | xargs -0 rm

[bash-4.1.5] ; find -name '*.log'

你可能要问了, 为什么要选 '\0' 而不是其他字符做分隔符呢? 这个也容易理解: 一般的编程语言中都用 '\0' 来作为字符串的结束标志, 文件的路径名中不可能包含 '\0' 字符.

其他我收集的find、xargs实例:

删除以html结尾的10天前的文件,包括带空格的文件:

find /usr/local/backups -name "*.html" -mtime +10 -print0 |xargs -0 rm -rfv

find /usr/local/backups -mtime +10 -name "*.html" -exec rm -rf {} \;

find -print 和 -print0的区别:

-print 在每一个输出后会添加一个回车换行符,而-print0则不会。

当前目录下文件从大到小排序(包括隐藏文件),文件名不为".":

find . -maxdepth 1 ! -name "." -print0 | xargs -0 du -b | sort -nr | head -10 | nl

nl:可以为输出列加上编号,与cat -n相似,但空行不编号

以下功能同上,但不包括隐藏文件:

for file in *; do du -b "$file"; done|sort -nr|head -10|nl

xargs结合sed替换:

find . -name "*.txt" -print0 | xargs -0 sed -i 's/aaa/bbb/g'

xargs结合grep:

find . -name '*.txt' -type f -print0 |xargs -0 grep -n 'aaa'    #“-n”输出行号

-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 con-
              tain a newline, then you should seriously consider using the -print0 option instead of -print.  See the UNUSUAL FILENAMES section for information about how unusual characters 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 corresponds to the -0 option of xargs.

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

00011 - find中的-print0和xargs中-0的奥妙的更多相关文章

  1. find中的-print0和xargs中-0的奥妙【转】

    find cygnus/firmware_cygnus/target/linux/brcm5830/files/arch/arm/mach-iproc/pm_iproc/ -name "*. ...

  2. find中的-print0和xargs中-0的奥妙

    原文地址:find中的-print0和xargs中-0的奥妙作者:改变自己 默认情况下, find 每输出一个文件名, 后面都会接着输出一个换行符 ('n'), 因此我们看到的 find 的输出都是一 ...

  3. find中的-print0和xargs中-0的区别

    默认情况下, find 每输出一个文件名, 后面都会接着输出一个换行符 ('\n'), 因此我们看到的 find 的输出都是一行一行的: [bash-4.1.5] ; ls -l total 0 -r ...

  4. linux find中的-print0和xargs中-0的奥妙

    默认情况下, find 每输出一个文件名, 后面都会接着输出一个换行符 ('n'), 因此我们看到的 find 的输出都是一行一行的: 比如我想把所有的 .log 文件删掉, 可以这样配合 xargs ...

  5. linux find命令-print0和xargs中-0使用技巧(转载)

    本文介绍了linux find命令中-print0和xargs中-0用法技巧,一些find命令的使用经验,需要的朋友参考下. 本节内容:linux find命令中-print0和xargs中-0的用法 ...

  6. linux find命令中-print0和xargs中-0的用法

    linux find命令中-print0和xargs中-0的用法. 1.默认情况下, find命令每输出一个文件名, 后面都会接着输出一个换行符 ('\n'), 因此find 的输出都是一行一行的: ...

  7. linux find命令-print0和xargs中-0使用技巧

    文章是转载的,原文很精彩,我对其中个别地方没有快速理解,我在此予以补充,方便后续回顾理解. 本文介绍了linux find命令中-print0和xargs中-0用法技巧,一些find命令的使用经验,需 ...

  8. find命令中的print0和xargs -0

    看到命令find . -name checkout-cache -f -- 不明白其中-print0和 xargs -0的用法.查了一下,转载一篇备忘. xargs命令的作用是将参数列表转换成小块分段 ...

  9. 地图四叉树一般用在GIS中,在游戏寻路中2D游戏中一般用2维数组就够了

    地图四叉树一般用在GIS中,在游戏寻路中2D游戏中一般用2维数组就够了 四叉树对于区域查询,效率比较高. 原理图

随机推荐

  1. 配置java环境变量【搭建java开发环境】【Path追加字符串,CLASSPATH填入固定的字符串】

    准备工作:把jdk压缩包解压出来 jdk的安装目录就是F:\a-bao\jdk1.8.0_65 第一步: 变量名:JAVA_HOME变量值:JDK安装路径 直接追加这些字符串 %JAVA_HOME%\ ...

  2. Cassandra Demo--Python操作cassandra

    ================================================================ 创建keyspace和table CREATE KEYSPACE ex ...

  3. Java开发培训基础知识解析之反射机制

    Java是老牌编程语言,是当前应用最广泛的编程语言之一.想要学习Java你就一定要掌握Java基础知识,而反射对于初学Java的人来说绝对是非常重要的知识点.什么是反射?如何理解反射机制?如何使用反射 ...

  4. vue的watcher 关于数组和对象

    数组 不能被监听到的情况 1.直接下标赋值(但对象直接修改原有属性值可以渲染视图,虽然也监听不到) 2.修改数组length 解决方法: this.$set(this.arr,index,val) p ...

  5. day10 内容大纲

    01 去年内容回顾 01 去年内容回顾 *args **kwargs: 万能参数,动态参数 * 魔性用法: 函数的定义时,* ** 聚合. 函数的执行时,* ** 打散. 形参顺序: 位置参数,*ar ...

  6. str的用法

    ##   字符串的索引切片以及常用的操作方法都是形成新的字符串,和原字符串没有关系 #  切片和索引 # 按照索引取值 #按照切片取值 ,顾头不顾尾, #切片加步长  # 只要倒叙取值就要加上反向步长 ...

  7. css中权重与继承

    出处:http://blog.csdn.net/xf616510229/article/details/53613212

  8. bzoj 2806 [Ctsc2012]Cheat——广义后缀自动机+单调队列优化DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2806 只想着怎么用后缀数据结构做,其实应该考虑结合其他算法. 可以二分那个长度 L .设当前 ...

  9. LOJ 2743(洛谷 4365) 「九省联考 2018」秘密袭击——整体DP+插值思想

    题目:https://loj.ac/problem/2473 https://www.luogu.org/problemnew/show/P4365 参考:https://blog.csdn.net/ ...

  10. HDU3501——欧拉函数裸题

    给整数N(1 ≤ N ≤ 1000000000),求小于N的与N不互素的所有正整数的和. 思路:1.用欧拉函数求出小于N的与N互素的正整数的个数: 2.若 p 与 N 互素,则 N-p 必与 N 互素 ...