默认情况下, 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 字符 ('')而不是换行符, 然后再告诉 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'

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

以上转自:http://blog.163.com/laser_meng@126/blog/static/16972784420117102638257/

其他我收集的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”输出行号

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 每输出一个文件名, 后面都会接着输出一个换行符 ('\n'), 因此我们看到的 find 的输出都是一行一行的: [bash-4.1.5] ; ls -l total 0 -r ...

  3. 00011 - 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. 丙申年把真假美猴王囚禁在容器中跑 ASP.NET Core 1.0

    var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...

随机推荐

  1. django学习1

    参考资料:http://www.cnblogs.com/feixuelove1009/p/5910384.html#top 本学习只在记录过程,需要更全面的资料,可直接上参考资料细看. django版 ...

  2. (转)关于RTP时间戳及多媒体通信同步的问题

    下载 多媒体通信同步方法,主要有时间戳同步法.同步标记法.多路复用同步法三种.下面主要讨论时间戳同步法,特别是 RTP 时间戳同步.内容包括 RTP 媒体间同步的实现,为什么需要 RTCP 的 NTP ...

  3. Python——pyiso8601

    该模块不是Python内建的模块,为Python补充了 ISO 8601 解析——将常见的 ISO 8601 日期字符创转化为 Python 的 datetime 对象. 安装 $ pip insta ...

  4. C# Bitmap/png转成jpg格式,压缩图片

    public static ImageCodecInfo GetEncoder(ImageFormat format) { ImageCodecInfo[] codecs = ImageCodecIn ...

  5. PHP替换回车换行的三种方法

    一个小小的换行,其实在不同的平台有着不同的实现,为什么要这样,世界是多样的! 本来在Unix世界换行用/n来代替换行, Windows为了体现不同,就用/r/n, 更有意思的是,Mac中又用了/r. ...

  6. iOS:获取 NSDate 的年

    NSDate *currentDate = [NSDate date]; NSCalendar* calendar = [NSCalendar currentCalendar]; NSDateComp ...

  7. svn管理码云项目

    1.设置SVN管理项目 进入项目->管理 2.获取SVN地址 3.SVN添加项目.单击右键 -> 检出->版本库Url(这里填写svn地址)

  8. Capability Model

    Data Scientist, Analytics We’re looking for data scientists to work on our core and business product ...

  9. mysql 两台主主复制配置

    A.服务器 [mysqld] # The TCP/IP Port the MySQL Server will listen on port= server-id= #master-host=10.1. ...

  10. 使用T4模板调用Sqlserver链接生成自己的模板

    <#@ template debug="false" hostspecific="false" language="C#" #> ...