繁体   English   中英

使用正则表达式 OR 和 find 列出和删除文件

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM