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 的输出都是一行一行的: ...
随机推荐
- 读文件名,shell
参考文献:(忘了哪个笔记了)http://www.docin.com/p-871820919.html
- opengl去除控制台黑窗口
增加如下语句: #pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\ ...
- hexo的环境搭建
今天开始折腾下hexo,安装起来还是有点坑,简单记录下,会不断更新. 网上安装的文章多不胜数,当然首先还是得去看看官方的文档. 按照官方的文档,不知大家是否顺利,本人搭建环境的时候并不顺利. 明确要安 ...
- appium python实例脚本1
#coding=utf-8import os, time, unittestfrom appium import webdriver PATH = lambda p:os.path.abspath(o ...
- js去重复和取重复数据
js数组中取重复数据的方法: 方法一:去重复数据 <script> Array.prototype.distinct=function(){ var a=[],b=[]; for(var ...
- Oracle数据库安装完成后相关问题的解决
笔者一直以来都是使用公司服务器上的oracle数据库,突然一天公司服务器宕机了,项目无法访问数据库跟着瘫痪了,所以准备在自己的机器上安装一个oracle数据库. 从官网下载安装了oracle 11g后 ...
- web 优化原则
1. 减少http 请求 2. 使用CDN 3. 添加expires 头 4. gzip 压缩 5. 样式表放在头部 6. 脚本放底部 7. 避免css 表达式 8. 使用 ...
- 最短路径问题的Dijkstra算法
问题 最短路径问题的Dijkstra算法 是由荷兰计算机科学家艾兹赫尔·戴克斯特拉提出.迪科斯彻算法使用了广度优先搜索解决非负权有向图的单源最短路径问题,算法终于得到一个最短路径树> ...
- 基于C#的UDP协议的同步实现
一.摘要 总结基于C#的UDP协议的同步通信. 二.实验平台 Visual Studio 2010 三.实验原理 UDP传输协议同TCP传输协议的区别可查阅相关文档,此处不再赘述. 四.实例 4.1 ...
- 【转】python 字符编码与解码——unicode、str和中文:UnicodeDecodeError: 'ascii' codec can't decode
原文网址:http://blog.csdn.net/trochiluses/article/details/16825269 摘要:在进行python脚本的编写时,如果我们用python来处理网页数据 ...