find 用xargs來處理帶空格的文件名文件

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


發佈了133 篇原創文章 · 獲贊 125 · 訪問量 39萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章