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 的输出都是一行一行的: ...
随机推荐
- Azure .Net应用架构原型
本文介绍一种部署在azure 上.net 应用的一种常用架构对接者.这个角色可以是Api GateWay或代理或负载均衡器.目的有多方面,集中管理机器的注册和监控,安全,负载均衡,请求过滤,反向代理等 ...
- php 路径问题
今天迁移网站服务器时,路径出错: 解决办法: 在网站首页根目录 设置define 全局变量 ——根目录: 在涉及目录的地方,统统替换为 根目录:
- python爬虫入门(6)-Scrapy基本使用
源码:链接:http://pan.baidu.com/s/1dEK82hb 密码:9flo 创建项目 scrapy startpro ...
- Objective-C-----协议protocol,代码块block,分类category
概述 ObjC的语法主要基于smalltalk进行设计的,除了提供常规的面向对象特性外,还增加了很多其他特性,本文将重点介绍objective-C中一些常用的语法特性. 当然这些内容虽然和其他高级语言 ...
- Android编程 高德地图 中如何重写 定位按键 的触发事件 (com.amap.api.maps2d.LocationSource)点击定位后不仅定位在地图中心点上而且可以设置地图的缩放大小和提示
在利用高德地图来编写自己的APP的时候,发现了一种对定位按键的重写方法,那就是利用 com.amap.api.maps2d.LocationSource 接口来重写. 什么是定位按键呢,下图中右 ...
- js之3D轮播图
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- ASP.NET网站发布时的那些坑
开发工具:VS2010,MVC4.0,SQLSERVER2008 服务器:Windows server 2012,IIS8,SQLSERVER2012 一.发布后,每个页面第一次打开都很卡,50秒或更 ...
- Spring 管理Filter和Servlet
本文转载自:http://www.open-open.com/lib/view/open1417248512252.html 在使用spring容器的web应用中,业务对象间的依赖关系都可以用cont ...
- java运行jar命令提示没有主清单属性和找不到主类
推荐一个java运行jar命令提示没有主清单属性的百度经验的链接:https://jingyan.baidu.com/article/db55b60990f6084ba30a2fb8.html jav ...
- 二次剩余-Cipolla
二次剩余 \(Cipolla\) 算法 概述 大概就是在模 \(p\) 意义下开根号,如求解方程\(x^2\equiv n(mod\ p)\). 这里只考虑 \(p\) 为素数的情况.若 \(p=2\ ...