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 的输出都是一行一行的: ...
随机推荐
- Scrapy爬虫库使用初体验
安装pip install Scrapy 中间可能会遇到的问题: 超时,网络问题需要多次尝试 缺少vc++库,官网可以下载 win32api缺失,https://sourceforge.net/pro ...
- iOS【野路子】精准获取webView内容高度,自适应高度
关于WebView内容高度的获取,相信很多人都踩过坑,无法获取到准确高度,导致页面布局出现差错,搜到的资料很多但都无法解决问题,以下是个人经验总结: 项目需求实现H5文章&原生评论效果,文章是 ...
- Jmeter简单的操作数据库
mysql驱动包下载地址: https://dev.mysql.com/downloads/connector/j/ 1.添加驱动配置,把下载下来的驱动配置上去 2.添加‘配置元件-用户定义的变量’, ...
- 如何创建一个基于命令行工具的跨平台的 NuGet 工具包
命令行可是跨进程通信的一种非常方便的手段呢,只需启动一个进程传入一些参数即可完成一些很复杂的任务.NuGet 为我们提供了一种自动导入 .props 和 .targets 的方法,同时还是一个 .NE ...
- Visual->UIElement->FrameworkElement,带来更多功能的同时也带来了更多的限制
在 WPF 或 UWP 中,我们平时开发所遇到的那些 UI 控件或组件,都直接或间接继承自 Framework.例如:Grid.StackPanel.Canvas.Border.Image.Butto ...
- iPhone4s 9.2.1安装cydia(越狱)
依据网上资料而来 主要就是上图视频中的6步骤选项,不要选错. 1.prepare for jailbreak 2.选择Accept 3. proceed with jailbreak 4.begin ...
- sublime自动格式化代码插件HTML-CSS-JS Prettify安装
sublime自动格式化代码插件HTML-CSS-JS Prettify安装 问题: 用 Sublime Text 格式化代码(安装 HTML-CSS-JS Prettify 插件)时,格式化时却会提 ...
- __all__ 作用, 相当于导入*
它是一个string元素组成的list变量,定义了当你使用 from <module> import * 导入某个模块的时候能导出的符号(这里代表变量,函数,类等) 参考文章: http: ...
- c#代码加密
源代码保护:怎样利用MaxtoCode加密dotNet源代码 http://www.webkaka.com/blog/archives/MaxtoCode-encrypt-dotnet-program ...
- Eclipse下无法解析注解:@Getter和@Setter
接触到一个项目,java bean全部使用@Getter和@Setter来偷懒,我用getXXX方法,结果发现编译失败,没法用.后来看到另一个项目也是用了@Getter和@Setter注解,但人家用的 ...