bash循环浏览没有扩展名的文件

时间:2020-06-08 10:56:59

标签: linux bash

与:Loop through all the files with a specific extension

我要遍历与模式匹配的文件:

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

我有一个文件列表,例如:

bob
bobob
bobob.log

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

bob
bobob

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

2 个答案:

答案 0 :(得分:2)

bash中,您可以使用xtended globbing的功能:

shopt -s extglob

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

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

答案 1 :(得分:1)

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

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