简体   繁体   English

删除与特定字符串格式不匹配的文件

[英]Delete files that don't match a particular string format

I have a set of files that are named similarly: 我有一组名称类似的文件:

TEXT_TEXT_YYYYMMDD

Example file name: 示例文件名:

My_House_20170426

I'm trying to delete all files that don't match this format. 我正在尝试删除所有与此格式不匹配的文件。 Every file should have a string of text followed by an underscore, followed by another string of text and another underscore, then a date stamp of YYYYMMDD. 每个文件都应包含一个文本字符串,后跟一个下划线,然后是另一个文本字符串和另一个下划线,然后是一个YYYYMMDD的日期戳。

Can someone provide some advice on how to build a find or a remove statement that will delete files that don't match this format? 有人可以提供一些建议,说明如何构建findremove语句以删除不符合此格式的文件吗?

Using find , add -delete to the end once you're sure it works. 确定可以使用后,请使用find ,在最后添加-delete

# gnu find
find . -regextype posix-egrep -type f -not -iregex '.*/[a-z]+_[a-z]+_[0-9]{8}'

# OSX find
find -E . -type f -not -iregex '.*/[a-z]+_[a-z]+_[0-9]{8}'

Intentionally only matching alphabetical characters for TEXT. 故意只为TEXT匹配字母字符。 Add 0-9 to each TEXT area like this [a-z0-9] if you need numbers. 如果需要数字,请像这样[a-z0-9]在每个TEXT区域中添加0-9

grep -v '(pattern)'

will filter out lines that match a pattern, leaving those that don't match. 会滤除与模式匹配的行,而留下不匹配的行。 You might try piping in the output of ls . 您可以尝试在ls的输出中进行管道ls And if you're particularly brave, you could pipe the output to something like xargs rm . 如果您特别勇敢,则可以将输出传递给xargs rm类的东西。 But deleting is kinda scary, so maybe save the output to a file first, look at it, then delete the files listed. 但是删除有点吓人,因此也许先将输出保存到文件中,查看它,然后删除列出的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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