【发布时间】:2013-11-20 14:35:15
【问题描述】:
我需要查找并归档具有特定文件名的文件,例如ABC_000.jpg
find ~/my-documents/ -iname "ABC_***.JPG" -type f -exec cp {} ~/my-documents/archive/ \;
但是我似乎无法找到一种方法来限制 find 函数仅查找 3 个整数,因为有些文件名为 ABC_CBA.jpg 我不想包含在其中
【问题讨论】:
我需要查找并归档具有特定文件名的文件,例如ABC_000.jpg
find ~/my-documents/ -iname "ABC_***.JPG" -type f -exec cp {} ~/my-documents/archive/ \;
但是我似乎无法找到一种方法来限制 find 函数仅查找 3 个整数,因为有些文件名为 ABC_CBA.jpg 我不想包含在其中
【问题讨论】:
试试这个发现:
find ~/my-documents/ -iname "ABC_[0-9][0-9][0-9].JPG" -type f -exec cp '{}' ~/my-documents/archive/ \;
编辑:或使用正则表达式:
find -E ~/my-documents/ -iregex ".*ABC_[0-9]{3}\.JPG" -type f -exec cp '{}' ~/my-documents/archive/ \;
【讨论】:
-regex 匹配特定名称(如果更复杂,则很有用):find ~/my-documents/ -iregex "ABC_[0-9]{3}.JPG" -type f -exec cp '{}' ~/my-documents
man find 时,我突然想到find -E 在这种情况下会参加聚会。答案已编辑。