Raphael Rafatpanah
Raphael Rafatpanah

Reputation: 19967

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?

Upvotes: 2

Views: 2250

Answers (3)

PesaThe
PesaThe

Reputation: 7499

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

Upvotes: 1

stackoverflow
stackoverflow

Reputation: 39

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

Upvotes: 1

SMA
SMA

Reputation: 37023

It should be simple like:

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

Upvotes: 2

Related Questions