當用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