【问题标题】:Using find command on in a Bash Script to find integers在 Bash 脚本中使用 find 命令查找整数
【发布时间】: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 我不想包含在其中

【问题讨论】:

    标签: linux bash find


    【解决方案1】:

    试试这个发现:

    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
    • @Bentoy13:谢谢,我实际上打算发布与我原来的答案相同的内容,但它在 OSX 上对我不起作用,我的工作很忙。现在,当我有时间再次查看man find 时,我突然想到find -E 在这种情况下会参加聚会。答案已编辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-21
    • 2012-10-30
    • 1970-01-01
    相关资源
    最近更新 更多