作者eyway8312 (湘缘)
看板Linux
标题[问题] 删除特定档案&内容外档案
时间Wed Apr 24 08:40:50 2019
如题 小弟我是新手 在此虚心求教 昨天研究了一番
假设我的目录内有 4个档案1个目录
ABC1.log ABC2.log ABC3.log unused.log
abc
其中abc为资料夹
ABC1.log ABC3.log 这两个档案内有 #try 的字串
假设我要保留有ABC开头档案且里面要有 #try字串
其余皆删除怎麽做比较好
小弟我研究很久只写出找到有这两个条件的档案以及不符合的档案
find . -name "ABC*" -exec grep -H "#try" {} \;
找到档案内有#try 即ABC1.log ABC3.log
find . ! -name "ABC*" ! -name "abc "
没有ABC字串也没有abc字串的档案 即unused.log
诚心向板上大大询问 在此感激不尽
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 115.82.82.62
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Linux/M.1556066452.A.8C7.html
※ 编辑: eyway8312 (115.82.82.62), 04/24/2019 08:41:05
1F:推 rickieyang: for i in `find . -type f ! -name "abc*"`; do 04/24 09:38
2F:→ rickieyang: if (! grep "#try" $i) the rm -f $i; fi; done 04/24 09:40
3F:→ rickieyang: if (! grep "#try" $i) then rm -f $i; fi; done 04/24 09:40
4F:→ eyway8312: 先谢谢r大 我回家试试看研究研究 04/24 09:42
5F:→ rickieyang: abc 下的档案要处理吗? 不需要的话 find 加 -maxdepth 04/24 09:42
6F:→ rickieyang: 把 $i 改成 "$i" 吧... 避免档名有空格 04/24 09:44
7F:→ eyway8312: abc忘记说 是空目录 04/24 09:44