find -exec 与xargs 区别
find . -name "*.txt" -exec rm {} \;
find . -name "*.txt" | xargs rm {}?
-exec
? ? 1.參數(shù)是一個一個傳遞的,傳遞一個參數(shù)執(zhí)行一次rm
? ? 2.文件名有空格等特殊字符也能處理
-xargs?
? ? 1.一次將參數(shù)傳給命令,可以使用-n控制參數(shù)個數(shù)
? ? 2.處理特殊文件名需要采用如下方式:
? ? find . -name "*.txt" print0 |xargs -0 rm {}?
實驗結(jié)果如下,可以清楚看到參數(shù)傳遞過程
[cpp] view plain copy
技巧: find -print0 ?與 xargs -0 的結(jié)合避免文件名有特殊字符如空格,引號等無法處理:
?find . -name "*.txt" print0 |xargs -0 rm {}?
find?
? ? ? ?-print True; print the full file name on the standard output, followed by a newline. ? If you are piping the
? ? ? ? ? ? ? output of find into another program and there is the faintest possibility that the ?files ?which ?you
? ? ? ? ? ? ? are ?searching ?for ?might ?contain ?a ?newline, then you should seriously consider using the -print0
? ? ? ? ? ? ? option instead of -print. ?See the UNUSUAL FILENAMES section for information about how unusual ?char-
? ? ? ? ? ? ? acters in filenames are handled.
? ? ? ?-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 contain newlines or other types ?of
? ? ? ? ? ? ? white space to be correctly interpreted by programs that process the find output. ?This option corre-
? ? ? ? ? ? ? sponds to the -0 option of xargs.
xargs?
? ? ? ?-0 ? ? Input items are terminated by a null ?character ?insteadof ?by
? ? ?whitespace, ?and the quotes and backslash are not special (every
? ? ?character is taken literally). ?Disables the end of file string,
? ? ?which ?istreated ?likeany 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.
總結(jié)
以上是生活随笔為你收集整理的find -exec 与xargs 区别的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 几个常用的文本处理shell 命令:fi
- 下一篇: Linux下ps -ef和ps aux的