find中的-print0和xargs中-0的奥妙
默认情况下, 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
比如我想把所有的 .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] ; 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 字符 ('')而不是换行符, 然后再告诉 xargs 也用 NULL 字符来作为记录的分隔符. 这就是 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'
你可能要问了, 为什么要选 '' 而不是其他字符做分隔符呢? 这个也容易理解: 一般的编程语言中都用 '' 来作为字符串的结束标志, 文件的路径名中不可能包含 '' 字符.
以上转自:http://blog.163.com/laser_meng@126/blog/static/16972784420117102638257/
其他我收集的find、xargs实例:
删除以html结尾的10天前的文件,包括带空格的文件:
find /usr/local/backups -name "*.html" -mtime +10 -print0 |xargs -0 rm -rfv
find /usr/local/backups -mtime +10 -name "*.html" -exec rm -rf {} ;
find -print 和 -print0的区别:
-print 在每一个输出后会添加一个回车换行符,而-print0则不会。
当前目录下文件从大到小排序(包括隐藏文件),文件名不为".":
find . -maxdepth 1 ! -name "." -print0 | xargs -0 du -b | sort -nr | head -10 | nl
nl:可以为输出列加上编号,与cat -n相似,但空行不编号
以下功能同上,但不包括隐藏文件:
for file in *; do du -b "$file"; done|sort -nr|head -10|nl
xargs结合sed替换:
find . -name "*.txt" -print0 | xargs -0 sed -i 's/aaa/bbb/g'
xargs结合grep:
find . -name '*.txt' -type f -print0 |xargs -0 grep -n 'aaa' #“-n”输出行号
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 每输出一个文件名, 后面都会接着输出一个换行符 ('\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 ...
- 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 的输出都是一行一行的: ...
- linux find命令-print0和xargs中-0使用技巧
文章是转载的,原文很精彩,我对其中个别地方没有快速理解,我在此予以补充,方便后续回顾理解. 本文介绍了linux find命令中-print0和xargs中-0用法技巧,一些find命令的使用经验,需 ...
- find命令中的print0和xargs -0
看到命令find . -name checkout-cache -f -- 不明白其中-print0和 xargs -0的用法.查了一下,转载一篇备忘. xargs命令的作用是将参数列表转换成小块分段 ...
- 丙申年把真假美猴王囚禁在容器中跑 ASP.NET Core 1.0
var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...
随机推荐
- rqalpha-自动量化交易系统(一)
因为最近做的东西牵涉到自动计算这一块,在网上搜了一下,基本上python做自动量化交易成了一个趋势,于是花了两天学习一下. 目标很简单,学习,使用. rqalpha看起来是比较成熟的,这儿看重的是自带 ...
- java 项目 存入mysql后 变问号 MySql 5.6 (X64) 解压版 1067错误与编码问题的解决方案
[参考]MySQL 5.7.19 忘记密码 重置密码 my.ini示例 服务启动后停止 环境 Java环境JDK1.8 安装好了 mysql-5.6.38-winx64 idea2016(64) ...
- windows上開啟多個apache服務器
1.安裝apache(這裡我用的是集成環境) 比較php版本 5.6 與 7.2 比較mysql版本 拓展: 注意:對個不同的版本的mysql,命令行進入,需要指明端口號,如:mysql -uroo ...
- u3d一个GameObject绑定两个AudioSource
u3d 一个GameObject绑定两个AudioSource ,使他们分别播放,并控制 using UnityEngine; using System.Collections; public cl ...
- Java多线程——Lock&Condition
Lock比传统线程模型中的synchronized方式更加面向对象,与生活中的锁类似,锁本身也应该是一个对象.两个线程执行的代码片段要实现同步互斥的效果,它们必须用同一个Lock对象. package ...
- [NSURL URLWithString:] returns nil
You need to escape the non-ASCII characters in your hardcoded URL as well: //localisationName is a a ...
- jquery click事件,多次执行
用jquery绑定一个按钮click事件后,第一次点击后,一切正常,第二次点击,竟然执行两次,以后越来越多, 后来查看文档发现 jquery click 不是 替换原有的function 而是接 ...
- Spring MVC异常处理详解 ExceptionHandler good
@ControllerAdvice(basePackageClasses = AcmeController.class) public class AcmeControllerAdvice exten ...
- lua中实现倒计时
今天在开发的时候,涉及到了使用倒计时来显示. 首先自己的思路是: 1.设计显示的Lable. 2.对传入的时间进行处理,转成字符串00:00:00.通过调用回调函数来控制一秒刷新一次. 转换算法: h ...
- 常用Linq示例代码
class Program { static void Main(string[] args) { //1. Aggregate int[] testArr = new int[] { 1, 2, ...