find命令中的print0和xargs -0
看到命令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的更多相关文章
- find中的-print0和xargs中-0的奥妙【转】
find cygnus/firmware_cygnus/target/linux/brcm5830/files/arch/arm/mach-iproc/pm_iproc/ -name "*. ...
- find中的-print0和xargs中-0的奥妙
原文地址:find中的-print0和xargs中-0的奥妙作者:改变自己 默认情况下, find 每输出一个文件名, 后面都会接着输出一个换行符 ('n'), 因此我们看到的 find 的输出都是一 ...
- find中的-print0和xargs中-0的区别
默认情况下, find 每输出一个文件名, 后面都会接着输出一个换行符 ('\n'), 因此我们看到的 find 的输出都是一行一行的: [bash-4.1.5] ; ls -l total 0 -r ...
- 00011 - find中的-print0和xargs中-0的奥妙
默认情况下, find 每输出一个文件名, 后面都会接着输出一个换行符 ('\n'), 因此我们看到的 find 的输出都是一行一行的: [bash-4.1.5] ; ls -l total 0 -r ...
- linux find中的-print0和xargs中-0的奥妙
默认情况下, find 每输出一个文件名, 后面都会接着输出一个换行符 ('n'), 因此我们看到的 find 的输出都是一行一行的: 比如我想把所有的 .log 文件删掉, 可以这样配合 xargs ...
- find -print0和xargs -0原理及用法
平常我们经常把find和xargs搭配使用,例如: find . -name "*.txt" | xargs rm 但是这个命令如果遇到文件名里有空格或者换行符,就会出错.因为xa ...
- Linux命令中,$、#、@、0、1、2、*、?的作用
$# 是传给脚本的参数个数 $0 是脚本本身的名字 $1 是传递给该shell脚本的第一个参数 $2 是传递给该shell脚本的第二个参数 $@ 是传给脚本的所有参数的列表 $* 是以 ...
- linux find命令-print0和xargs中-0使用技巧(转载)
本文介绍了linux find命令中-print0和xargs中-0用法技巧,一些find命令的使用经验,需要的朋友参考下. 本节内容:linux find命令中-print0和xargs中-0的用法 ...
- linux find命令中-print0和xargs中-0的用法
linux find命令中-print0和xargs中-0的用法. 1.默认情况下, find命令每输出一个文件名, 后面都会接着输出一个换行符 ('\n'), 因此find 的输出都是一行一行的: ...
随机推荐
- python常用模块之configparser模块
python常用模块之configparser 作用:解析配置文件 假设在当前目录下有这样一个conf.ini文件 [DEFAULT] ServerAliveInterval = 45 Compres ...
- BDD测试框架Spock概要
前言 为了找到一个适合自己的.更具操作性的.以DDD为核心的开发方法,我最近一直在摸索如何揉合BDD与DDD.围绕这个目标,我找到了Impact Mapping → Cucumber → Spock ...
- python 怎么画图
1 安装matplotlib: 安装方法:http://www.2cto.com/os/201309/246928.html(其中,安装过程中,tar解压怎么解都有问题.然后就删掉再下载一遍) 2 使 ...
- 实战maven私有仓库三部曲之三:Docker下搭建maven私有仓库
本章是<实战maven私有仓库>系列的第三篇,在前两章中,我们先在linux搭建maven私有仓库,然后在开发环境使用此仓库,本章我们在docker下快速搭建maven私有仓库,然后像前面 ...
- 优化器Optimizer
目前最流行的5种优化器:Momentum(动量优化).NAG(Nesterov梯度加速).AdaGrad.RMSProp.Adam,所有的优化算法都是在原始梯度下降算法的基础上增加惯性和环境感知因素进 ...
- Hbase rowkey热点问题
当处理由连续事件得到的数据时,即时间上连续的数据.这些数据可能来自于某个传感器网络.证券交易或者一个监控系统.它们显著的特点就是rowkey中含有事件发生时间.带来的一个问题便是HBase对于row的 ...
- Tornado的入门研究
1.为啥要了解Tornado 首先,Tornado是大神写出来的,如果学习python的话,参照Tornado的源码是一件非常好的事情,属于FaceBook的开源代码 其次,Tornado就是我们在 ...
- chrome扩展程序开发之在目标页面执行自己的JS
大家都知道JS是执行在client的.所以,假设我们自己写一个浏览器的话.是一定能够往下载下来的网页源码中加入js的.可惜我们没有这个能力.只是幸运的是,chrome的扩展程序能够帮我们做到这件事. ...
- cocos2dx ScrollView的用法
http://blog.csdn.net/u014096244/article/details/21525789 http://bbs.9ria.com/thread-199305-1-1.html ...
- Docker生态会重蹈Hadoop的覆辙吗?
从网上找到了这篇2016年中旬刷爆朋友圈的文章,没有找到作者和首发出处.两年多过去了,文中分析的很多不确定性都有了结论,里面不少分析思路.观点还是很不错的. Docker的兴起和Hadoop何其相似 ...