#####################################################################################################
#find ... -exec rm {} \;
可以把find命令查找到的結果刪除,簡單的說是把find發現的結果一次性傳給exec選項,這樣當文件數量較多的時候,就可能會出現“參數太多”之類的錯誤。
-exec 必須由一個 ; 結束,而因為通常 shell 都會對 ; 進行處理,所以用 \; 防止這種情況。{} 可能需要寫做 '{}',也是為了避免被 shell 過濾
eg1:
[root@localhost xusx]# touch 1.sh 2.sh 3.sh 1.txt 2.txt 3.txt
[root@localhost xusx]# ls
1.sh 1.txt 2.sh 2.txt 3.sh 3.txt passwd
[root@localhost xusx]# find ./ -type f ! -name "passwd" -exec rm {} \; (刪除passwd之外的文件)
[root@localhost xusx]# ls
1.sh 1.txt 2.sh 2.txt 3.sh 3.txt
#####################################################################################################
#find ... | xargs rm -rf
這個命令可以避免這個錯誤,因為xargs命令會分批次的處理結果。這樣看來,“find ... | xargs rm -rf”是更通用的方法,推薦使用!
rm不接受標准輸入,所以不能用find / -name "tmpfile" |rm
eg1:
[root@localhost xusx]# ls
1.sh 1.txt 2.sh 2.txt 3.sh 3.txt
[root@localhost xusx]# find ./ -type f -name "1.sh"|xargs rm -f
[root@localhost xusx]# ls
1.txt 2.sh 2.txt 3.sh 3.txt
#####################################################################################################
./表示從當前目錄找
-type f,表示只找file,文件類型的,目錄和其他字節不要
-exec 把find到的文件名作為參數傳遞給后面的命令行,代替{}的部分
-exec后跟的命令行,必須用“ \;”結束
#####################################################################################################
find ./ -type f -name "*.sh" -exec mv {} /opt/ \; =====>\轉意符號。否則 ; 不被shell識別。
mv `find ./ -type f -name "*.sh" ` /opt/ 或者 cp $(find ./ -type f -name "*.sh" ) /opt/
