看到命令find . -name '*.h' -print0 | xargs - checkout-cache -f --

不明白其中-print0和 xargs -0的用法。查了一下,转载一篇备忘。

xargs命令的作用是将参数列表转换成小块分段传递给其他命令,以避免参数列表过长的问题

以下内容转自http://blog.163.com/laser_meng@126/blog/static/16972784420117102638257/
默认情况下, find 每输出一个文件名, 后面都会接着输出一个换行符 ('\n'), 因此我们看到的 find 的输出都是一行一行的:
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.] ; ls -l
total
-rw-r--r-- root root -- : file1.log
-rw-r--r-- root root -- : file2.log
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.] ; find -name '*.log'
./file2.log
./file1.log
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.] ; bye 比如我想把所有的 .log 文件删掉, 可以这样配合 xargs 一起用:
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.] ; find -name '*.log'
./file2.log
./file1.log
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.] ; find -name '*.log' | xargs rm
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.] ; find -name '*.log'
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.] ; bye 嗯, 不错, find+xargs 真的很强大. 然而:
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.] ; ls -l
total
-rw-r--r-- root root -- : file .log
-rw-r--r-- root root -- : file .log
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.] ; find -name '*.log'
./file .log
./file .log
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.] ; find -name '*.log' | xargs rm
rm: cannot remove `./file': No such file or directory
rm: cannot remove `.log': No such file or directory
rm: cannot remove `./file': No such file or directory
rm: cannot remove `.log': No such file or directory
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.] ; bye 原因其实很简单, xargs 默认是以空白字符 (空格, TAB, 换行符) 来分割记录的, 因此文件名 ./file .log 被解释成了两个记录 ./file 和 .log, 不幸的是 rm 找不到这两个文件. 为了解决此类问题, 聪明的人想出了一个办法, 让 find 在打印出一个文件名之后接着输出一个 NULL 字符 ('\0') 而不是换行符, 然后再告诉 xargs 也用 NULL 字符来作为记录的分隔符. 这就是 find 的 -print0 和 xargs 的 - 的来历吧.
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.] ; ls -l
total
-rw-r--r-- root root -- : file .log
-rw-r--r-- root root -- : file .log
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.] ; find -name '*.log' -print0 | hd
A B C D E F |0123456789ABCDEF|
--------+--+--+--+--+---+--+--+--+---+--+--+--+---+--+--+--+--+----------------|
: 2e 2f 6c 2e 6c 6f 2e 2f |./file .log../f|
: 6c 2e 6c 6f |ile .log. |
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.] ; find -name '*.log' -print0 | xargs - rm
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.] ; find -name '*.log'
-(dearvoid@LinuxEden:Forum)-(~/tmp/find)-
[bash-4.1.] ; bye 你可能要问了, 为什么要选 '\0' 而不是其他字符做分隔符呢? 这个也容易理解: 一般的编程语言中都用 '\0' 来作为字符串的结束标志, 文件的路径名中不可能包含 '\0' 字符.

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. 00011 - find中的-print0和xargs中-0的奥妙

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

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

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

  6. find -print0和xargs -0原理及用法

    平常我们经常把find和xargs搭配使用,例如: find . -name "*.txt" | xargs rm 但是这个命令如果遇到文件名里有空格或者换行符,就会出错.因为xa ...

  7. Linux命令中,$、#、@、0、1、2、*、?的作用

    $# 是传给脚本的参数个数 $0 是脚本本身的名字   $1 是传递给该shell脚本的第一个参数   $2 是传递给该shell脚本的第二个参数   $@ 是传给脚本的所有参数的列表   $* 是以 ...

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

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

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

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

随机推荐

  1. python 多维list声明时的小问题

    a=[[]]*3 a Out[18]: [[], [], []] a[0].append(1) a Out[20]: [[1], [1], [1]] b=[[] for _ in range(3)] ...

  2. C# 加密狗 超级狗 加密程序 程序授权示例 程序授权验证

    本篇针对超级狗进行讲解,对应的超级狗套件的工具包版本为2.4版本.超级狗图片如下: 主要包含两个狗,一个是超级狗,一个是开发狗,在本博文中都是必须的.首先先安装光盘中的开发套间. 接下来就开始演示一个 ...

  3. java面试题大纲

    跳槽时时刻刻都在发生,但是我建议大家跳槽之前,先想清楚为什么要跳槽.切不可跟风,看到同事一个个都走了,自己也盲目的开始面试起来(期间也没有准备充分),到底是因为技术原因(影响自己的发展,偏移自己规划的 ...

  4. LNMP架构基础搭建

    LNMP架构+wordpress博客 环境: centos6.7 2.6.32-573.el6.x86_64 nginx-1.6.3 mysql-5.5.49 php-5.3.27 wordpress ...

  5. U盘传送容量与格式问题

    问题 今天想将7.6G的文件拷到U盘里,提示u盘内存不足,其实内存为14+G. 解答 U盘格式对于U盘的传送大小有限制 下面为U盘三种不同格式的应用及优缺点 FAT32格式:为系统默认格式,具有极佳的 ...

  6. manacher 算法 这个人确实写得太好了;

    O(n)回文子串(Manacher)算法 资料来源网络 参见:http://www.felix021.com/blog/read.php?2040 问题描述: 输入一个字符串,求出其中最大的回文子串. ...

  7. IIS反向代理/Rewrite/https卸载配置

    目标,使IIS具有类似与Nginx的功能,将指定域名的请求重定向到IIS内.IIS外.其他机器上的其他端口,并且实现https卸载功能 重点预告: 1.安装最新版urlrewrite(微软开发的)插件 ...

  8. cell的循环利用

    方式1 // 1.先根据cell的标识去缓存池中查找可循环利用的cell UITableViewCell *cell = [tableView dequeueReusableCellWithIdent ...

  9. vs中无法找到头文件

    在VS项目上右键属性 C/C++->常规->附加包含目录中把此路径添加上,路径与路径之间用 ; 隔开

  10. ballerina 学习十四 values && types

    ballerina 包含的数据类型有string int map array record boolean ojbect function table tuple any 简单说明 数据类型和其他语言 ...