xargs的基本使用也没必要多言,这里主要讲一下我使用过程当中遇到的问题:shell
问题出在用find找出的文件传给xargs处理时,却出现了问题。我简化了一下问题,以下:ui
]$ touch "a b" [lyu@swe-vm-kcr-pr01 ~]$ find . -maxdepth 1 -name "a*b" | xargs rm rm: cannot remove `./a': No such file or directory rm: cannot remove `b': No such file or directory
虽然文件名包含空格也是蛮奇葩的,可是世界上就是有各类各样奇葩的存在。这时候就等于rm a b,天然就报错了。this
那遇到这种问题该如何处理呢? 答案就是xargs的 -0选项。spa
--null, -0 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 option produces input suitable for this mode.
-0将会使用null来分割参数而不是空格,固然find的输入也须要使用null来分隔,不然仍是会出错:
code
$ find . -maxdepth 1 -name "a*b" | xargs -0 rm rm: cannot remove `./a b\n': No such file or directory
而find也提供 -print0功能。ci
$ find . -maxdepth 1 -name "a*b" -print0 | xargs -0 rm
其实xargs还有不少功能,像-L -I选项,不过我仍是用的不多,不过了解一下是最好的,等到用的时候能想到,再去细看不迟。rem