文章是转载的,原文很精彩,我对其中个别地方没有快速理解,我在此予以补充,方便后续回顾理解。

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

本节内容:
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的用法. 1.默认情况下, find命令每输出一个文件名, 后面都会接着输出一个换行符 ('\n'), 因此find 的输出都是一行一行的: ...

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

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

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

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

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

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

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

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

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

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

  8. (转)Linux将命令添加到PATH中

    转:https://www.cnblogs.com/leibg/p/4479921.html Linux将命令添加到PATH中博客分类:linuxLinuxApacheBash 简单说PATH就是一组 ...

  9. linux常用命令(19)find xargs

    在使用 find命令的-exec选项处理匹配到的文件时, find命令将所有匹配到的文件一起传递给exec执行.但有些系统对能够传递给exec的命令长度有限制,这样在find命令运行几分钟之后,就会出 ...

随机推荐

  1. MySQL 存储过程循环

    MySQL  存储过程循环 MySQL循环语句(包括WHILE,REPEAT和LOOP)来根据条件反复运行代码块. MySQL提供循环语句,允许您根据条件重复执行一个SQL代码块. MySQL中有三个 ...

  2. BeanNameAware和BeanFactoryAware接口

    平时写代码很少去实现这两个接口,这里只是做下了解,方便读Spring源码.BeanNameAware接口作用:让Bean对Name或id有知觉 package com.example.demo.biz ...

  3. 解决JS中取URL地址中的参数中文乱码

    GET请求会将中文编码,如果取出乱码的话,应该进行解码操作, 下面的函数是获取指定参数名的参数值,参数值可是中文.英文. function getQueryString(name) { var reg ...

  4. Lab 10-2

    The file for this lab is Lab10-02.exe. Questions and Short Answers Does this program create any file ...

  5. 另类AOP设计

    常见的AOP设计都基于Remoting的RealProxy,或者基于Emit实现的动态代理,或者基于反射的Attribute扫描拦截.但是我们还有另类的拦截方案DynamicObject,只要我们继承 ...

  6. 记一次oracle数据库复制过程

    记录一次自己数据库复制的过程(从公司测试环境复制到客户测试环境),主要是每次自己都会忘记,不如记录一下,方便自己以后找,因此,本篇内容不会很详细,主要是用于给我自己提醒,相对于一种记笔记的效果. cm ...

  7. SWUST OJ(1101)

    顺序表中的数据的循环移动 #include <iostream> #include <cstdlib> using namespace std; int main() { in ...

  8. what API can do

    APIs for manipulating documents loaded into the browser. The most obvious example is the DOM (Docume ...

  9. what is API

    JavaScript — A high-level scripting language built into browsers that allows you to implement functi ...

  10. 记一次简单的sql注入

     什么是sql注入攻击?  所谓SQL注入式攻击,就是攻击者把SQL命令插入到Web表单的输入域或页面请求的查询字符串,欺骗服务器执行恶意的SQL命令.在某些表单中,用户输入的内容直接用来构造(或者影 ...