使用正则表达式或查找来列出和删除文件
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
的输出,而我希望在将命令扩展到:
之前有 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
确保表达式在括号之间
我有一个包含这些文件的文件夹:
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
的输出,而我希望在将命令扩展到:
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
确保表达式在括号之间