find ./ -name *.bak | xargs rm/mv


#####################################################################################################

#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后跟的命令行,必须用“ \;”结束

#####################################################################################################

经典的移动:man mv
find ./  -type f -name "*.sh"|xargs mv  -t /opt/
find ./  -type f -name "*.sh"|xargs  -i mv  {} /opt/
find ./  -type f -name "*.sh"  -exec mv  {} /opt/  \;  =====>\转意符号。否则 ; 不被shell识别。
mv  `find ./  -type f -name "*.sh" ` /opt/  或者  cp $(find ./  -type f -name "*.sh"  ) /opt/

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM