find 用xargs来处理带空格的文件名文件

8 篇文章 0 订阅

当用find和xargs来处理文件时,如果文件名包含空格,会导致处理失败。

在find的帮助中,有一个参数-print0:

-print0
              True; print the full file name on the standard output, followed by a null character (instead of the newline character that -print uses).  This allows file names that con‐
              tain newlines or other types of white space to be correctly interpreted by programs that process the find output.  This option corresponds to the -0 option of xargs.

当用上这个参数时,如果文件名包含有空格类型的字符时,会用null字符来替换,从而不会被解释为换行符

而在xargs的帮助中,有-0这个参数:

-0, --null
              Input items are terminated by a null character instead of by whitespace, and the quotes and backslash are not special (every character is taken literally).  Disables  the
              end  of  file string, which is treated like any other argument.  Useful when input items might contain white space, quote marks, or backslashes.  The GNU find -print0 op‐
              tion produces input suitable for this mode.

这里最后有提到,是为了匹配像find -print0这样的模式,把null字符重新解释为空格

到了这里,我想大家就明白了,来个例子:

[root@localhost temp]# find . -type f -name test* -print0 | xargs -0 md5sum
e74a5cebda37864d767b48a6052ca2d8  ./test. php
[root@localhost temp]# md5sum ./test.\ php
e74a5cebda37864d767b48a6052ca2d8  ./test. php


  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值