xargs與find常常結合來進行文件操做,平時刪日誌的時候只是習慣的去刪除,好比html
# find . -type f -name "*.log" | xargs rm -rf *linux
就將以log結尾的文件刪除了,若是我想去移動或者複製就須要使用參數來代替了。ide
xargs -i 參數或者-I參數配合{}便可進行文件的操做。spa
-I replace-str日誌
Replace occurrences of replace-str in the initial-arguments with names read from standard input. Also, unquoted blanks do not terminatehtm
input items; instead the separator is the newline character. Implies -x and -L 1.ip
--replace[=replace-str], -i[replace-str]ci
This option is a synonym for -Ireplace-str if replace-str is specified, and for -I{} otherwise. This option is deprecated; use -I instead.get
man了一下看的仍是不太懂,經過例子,作做實驗將個人理解寫一下。input
############### 操做的目錄下的文件###############
[root@test05 ab]# ls
1kk.zip 3kk.zip 5kk.zip b.rar d.rar f.rar h.rar j.rar mini.txt ni.txt
2kk.zip 4kk.zip a.rar c.rar e.rar g.rar i.rar k.rar nii.txt
###################使用 i 參數 ##################
[root@test05 ab]# find . -type f -name "*.txt" | xargs -i cp {} /tmp/k/
[root@test05 ab]# ls ../k/
mini.txt nii.txt ni.txt
[root@test05 ab]#
################### 使用 I 參數 ################
[root@test05 ab]# find . -type f -name "*.txt" | xargs -I {} cp {} /tmp/n/
[root@test05 ab]# ls ../n/
mini.txt nii.txt ni.txt
結果出來了,
加-i 參數直接用 {}就能代替管道以前的標準輸出的內容;
加 -I 參數 須要事先指定替換字符