简体   繁体   中英

Script to loop through files except certain files

I'm trying to loop through all HTML files in a directory.

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 ?

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 :

shopt -s extglob nullglob
for f in !(index).html; do
    echo "$f"
done
  • shopt -s extglob : enables extended globbing
  • shopt -s nullglob : makes sure the loop is not executed should there be no matching files
  • !(index).html : expands to all html files that are not index.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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