[英]Using regex OR with find to list and delete files
我有一个包含这些文件的文件夹:
sample.jpg
sample.ods
sample.txt
sample.xlsx
现在,我需要查找并删除以.ods
或.xlsx
结尾的文件。
为了把它们捞出来,我最初使用:
ls | grep -E "*.ods|*.xlsx"
这给了我:
sample.ods
sample.xlsx
现在,我不想解析ls
所以我使用find
:
find . -type f -regextype grep -regex '.*/*.ods\|*.xlsx' | wc -l
但这给了我1
的 output 而我希望在将命令扩展为之前有2
文件:
find . -type f -regextype grep -regex '.*/*.ods\|*.xlsx' | xargs -d"\n" rm
哪个有效,但仅删除.ods
文件而不删除.xlsx
文件。
我在这里想念什么?
我在ubuntu 18.04
上,我的find
版本是find (GNU findutils) 4.7.0-git
。
您不需要在这里使用正则表达式,只需使用 -name 和 -or 等:
find . -type f -name "*.ods" -or -name "*.xlsx" -delete
查找以 ods 或 xlsx 结尾的文件并删除
如果您真的想使用正则表达式,可以使用以下内容:
find . -maxdepth 1 -regextype posix-extended -regex "(.*\.ods)|(.*\.xlsx)" -delete
确保表达式在括号之间
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.