关于xargs的小BUG

sh/bash/dash/ksh/zsh等Shell脚本
回复
iamcook84
帖子: 41
注册时间: 2013-08-29 9:27

关于xargs的小BUG

#1

帖子 iamcook84 » 2015-04-10 10:57

suse@linux-337q:~/downs> find . -name '*.txt'
./d.txt
./新建 文件夹/m.txt
suse@linux-337q:~/downs> find . -name '*.txt' | xargs rm -rf
suse@linux-337q:~/downs> find . -name '*.txt'
./新建 文件夹/m.txt

为什么删不掉m.txt?有问题吧。

我换一种方法:
suse@linux-337q:~/downs> find . -name '*.txt' -exec rm -rf {} \;
suse@linux-337q:~/downs> find . -name '*.txt'
suse@linux-337q:~/downs>
这回好了,为什么是这样呢?好像只有在带空格的中文文件夹中才会有此问题。
头像
astolia
论坛版主
帖子: 6421
注册时间: 2008-09-18 13:11

Re: 关于xargs的小BUG

#2

帖子 astolia » 2015-04-10 14:55

你不去读一下xargs的manpage么?
Because Unix filenames can contain blanks and new‐
lines, this default behaviour is often problematic;
filenames containing blanks and/or newlines are in‐
correctly processed by xargs. In these situations it
is better to use the -0 option, which prevents such
problems.
When using this option you will need to
ensure that the program which produces the input for
xargs also uses a null character as a separator. If
that program is GNU find for example, the -print0 op‐
tion does this for you.
回复