脚本循环遍历除某些文件之外的文件

时间:2018-01-23 15:59:48

标签: bash shell unix

我正在尝试遍历目录中的所有HTML个文件。

以下工作正常。

for f in *.html; do echo $f; done

但是,如何添加if条件,以便仅在文件名不等于index.html时才回显?

3 个答案:

答案 0 :(得分:1)

应该很简单:

for f in *.html
do 
    if [ "$f" != "index.html" ]
    then
        echo $f
    fi
done

答案 1 :(得分:1)

for f in *.html; do  [ "$f" != "index.html" ] && echo "$f"; done

答案 2 :(得分:1)

也可以使用extended globbing完全从列表中排除index.html

shopt -s extglob nullglob
for f in !(index).html; do
    echo "$f"
done
  • shopt -s extglob:启用扩展的globbing
  • shopt -s nullglob:如果没有匹配的文件,请确保不执行循环
  • !(index).html:展开到非html的所有index.html个文件
相关问题