[ 总结 ] 删除通过find查找到的文件

本文介绍了如何使用Shell命令批量删除特定命名的文件,并在目录下创建带有abc后缀的文件,涉及文件操作、命令执行及循环应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[root@cloud abc]# touch test{1,2,3,5}
[root@cloud abc]# ls
shadow  test  test1  test2  test3  test5
[root@cloud abc]# rm -rf `find . -name "test*"`      # 第一种方法
[root@cloud abc]# ls
shadow
[root@cloud abc]# touch test{1,2,3,5}
[root@cloud abc]# find . -name "test*" -exec rm {} \;  # 第二种方法
[root@cloud abc]# touch test{1,2,3,5}
[root@cloud abc]# find . -name "test*" | xargs rm     # 第三种方法
[root@cloud abc]# ls
shadow
[root@cloud abc]# ls
shadow  test  test1  test2  test3  test5
[root@cloud abc]# rm -rf `find . -name "test?"`  # 这里在扩展下,删除 查找对象+1个字符的文件。
[root@cloud abc]# ls
shadow  test
[root@cloud abc]# mkdir test{1..9}  # 创建test1-9
[root@cloud abc]# ls
test1  test2  test3  test4  test5  test6  test7  test8  test9
[root@cloud abc]# for i in `find . -type d ! -name .`;do echo "hehe" >> $i/abc && echo "$i is success."; done  # 在每个目录下创建一个abc的文件
./test4 is success.
./test3 is success.
./test8 is success.
./test2 is success.
./test6 is success.
./test1 is success.
./test5 is success.
./test7 is success.
./test9 is success.
[root@cloud abc]# ls
test1  test2  test3  test4  test5  test6  test7  test8  test9

[root@cloud abc]# cat  test{1..9}/abc  # 查看每个目录下文件的内容
hehe
hehe
hehe
hehe
hehe
hehe
hehe
hehe
hehe

 

转载于:https://www.cnblogs.com/hukey/p/5354841.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值