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

1、默认情况下, 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

比如用find命令把所有的 .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] touch "file 1.log"

[bash-4.1.5] touch "file 2.log"

[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 字符 ('') 而不是换行符 (-print0), 然后再告诉 xargs 也用 NULL 字符来作为记录的分隔符 (xargs -0). 这就是 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'
 
你可能要问了, 为什么要选 '' 而不是其他字符做分隔符呢? 这个也容易理解: 一般的编程语言中都用 '' 来作为字符串的结束标志, 文件的路径名中不可能包含 '' 字符.

解释下为什么 find -name '*.log' -print0 | xargs -0 rm 能删除:

先看 find -name '*.log' -print 的结果:

在看先看 find -name '*.log' -print0 的结果如下,可以发现换行符变成了 ''

然后看一下  find -name '*.log' -print0 | xargs -0 的输出结果:发现 xargs 将 '' 当作多条记录的分割符。

因此 file 1.log 和 file 2.log 就被分开处理了, file 1.log 会被作为一个文件处理,file 1.log就不会被拆开为 file 和 1.log 两个文件了。

至于为什么  file 1.log 和 file 2.log 会被分开处理,详情看这个文章

此篇文章为转载,谢谢查看!

linux find命令中-print0和xargs中-0的用法的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. 00011 - find中的-print0和xargs中-0的奥妙

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

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

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

  8. [Linux] sed命令使用之在文件中快速删除/增加指定行

    1.删除文档的第一行 sed -i '1d' <file> 2.删除文档的最后一行sed -i '$d' <file> 3.在文档指定行中增加一行例如文档如下:echo &qu ...

  9. Linux常用命令1-50(持续更新中)

    1:echo $PATH  (打印出PATH变量的值) 不同用户下面的PATH值有可能不一样 echo   有显示打印的意思 $         表示后面的是一个变量的意思 PATH  变量 /usr ...

随机推荐

  1. [LeetCode] Card Flipping Game 翻卡片游戏

    On a table are N cards, with a positive integer printed on the front and back of each card (possibly ...

  2. Docker 安装以及运用

    Docker 运行在 CentOS 7 上,要求系统为64位.系统内核版本为 3.10 以上.Docker 运行在 CentOS-6.5 或更高的版本的 CentOS 上,要求系统为64位.系统内核版 ...

  3. andorid简易定位

    权限: <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></use ...

  4. [POI2002][HAOI2007]反素数(Antiprime)

    题目链接 这道题需要用到整数唯一分解定理以及约数个数的计算公式.这里我就不再阐述了. 公式可以看出,只有指数影响约数个数,那么在唯一分解出的乘式中,指数放置的任何位置都是等价的.(即 23*34*57 ...

  5. 【Java】递归递推的应用

    利用阶乘公式来计算组合式: 程序设计思想: 根据公式来计算组合数的大小,从键盘输入n,k的值,设计一个计算阶乘的大小,如果输入的数a为1或0,则直接return 1,否则运用递归,计算a-1的阶乘,直 ...

  6. jsplumb 使用总结

    1 删除连线问题 funcion clearDrawGraph { if (this.graphInstance !== null) { const connections = this.graphI ...

  7. Apache Arrow

    https://www.kdnuggets.com/2017/02/apache-arrow-parquet-columnar-data.html https://arrow.apache.org/ ...

  8. kubernetes in action - Overview

    传统的应用都是“monoliths”,意思就是大应用,即所有逻辑和模块都耦合在一起的 这样明显很挺多问题的,比如只能scale up,升级必须整体升级,扩容 所以我们就想把大应用,broken dow ...

  9. less的使用(好文章)

    好文章链接:30分钟学会less 自己总结一下这篇文章中的几个关键字:变量.混合.函数.嵌套.@import 下面贴上自己照着上面写的一些代码: <template> <div cl ...

  10. Windows Server 2012 R2服务器部署Tomcat JDK、安装Mysql以及将Java项目部署到CVM

    我们平时所调试的Java Web 项目需要在本地Eclipse或者MyEclipse当中开发调试,并且部署到Tomcat上来测试,比如说笔者这里用的eclipse添加tomcat服务器, 但是这里发布 ...