【问题标题】:bash loop through files that have no extensionbash 循环遍历没有扩展名的文件
【发布时间】:2020-09-27 07:47:05
【问题描述】:

相关:Loop through all the files with a specific extension

我想遍历匹配某个模式的文件:

for item in ./bob* ; do
    echo $item
done

我有一个像这样的文件列表:

bob
bobob
bobob.log

我只想列出没有扩展名的文件:

bob
bobob

存档的最佳方式是什么? - 我可以在循环中以某种方式执行,还是需要在循环中使用 if 语句?

【问题讨论】:

    标签: linux bash


    【解决方案1】:

    bash你可以使用xtended globbing的功能:

    shopt -s extglob
    
    for item in ./bob!(*.*) ; do
        echo $item
    done
    

    您可以将shopt -s extglob 放入您的.bashrc 文件中以启用它。

    【讨论】:

    • thanks 效果很好 - 另一个答案也是如此,我有点武断地接受了这个,因为我更喜欢语法,但两者都很好
    【解决方案2】:

    最近的 Bash 版本支持正则表达式:

    for f in *
    do
      if [[ "$f" =~ .*\..*  ]]
      then
        : ignore
      else
        echo "$f"
      fi
    done
    

    【讨论】:

    • thanks 效果很好 - 其他答案也是如此,我无法确定哪个“接受的答案”。所以我只是接受我使用的答案,因为它的内容更少(大约 1!?)并且更容易阅读。
    猜你喜欢
    • 2018-10-14
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    • 2016-09-12
    • 2013-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多