简体   繁体   English

遍历除某些文件以外的文件的脚本

[英]Script to loop through files except certain files

I'm trying to loop through all HTML files in a directory. 我试图遍历目录中的所有HTML文件。

The following works just fine for that. 以下工作就可以了。

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

But, how can I add an if condition so that the file name is only echoed if it is not equal to index.html ? 但是,如何添加if条件,以便仅在不等于index.html文件名?

It should be simple like: 它应该像这样简单:

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

It's also possible to exclude index.html from the list entirely with extended globbing : 也可以通过扩展的globbing从列表中完全排除index.html

shopt -s extglob nullglob
for f in !(index).html; do
    echo "$f"
done
  • shopt -s extglob : enables extended globbing shopt -s extglob :启用扩展的shopt -s extglob
  • shopt -s nullglob : makes sure the loop is not executed should there be no matching files shopt -s nullglob :确保在没有匹配文件的情况下不执行循环
  • !(index).html : expands to all html files that are not index.html !(index).html :扩展到所有不是index.html html文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM