-1

How can I test a directory to see if it contains files typically used for a website? I've searched and found how to test for one extension at a time, which led me to do this:

c1=`ls -1 /var/www/*.htm 2>/dev/null | wc -l`
c2=`ls -1 /var/www/*.html 2>/dev/null | wc -l`
c3=`ls -1 /var/www/*.php 2>/dev/null | wc -l`
if [ $c1 != 0 ] || [ $c2 != 0 ] || [ $c3 != 0 ]; then    
    echo true
fi 

However, this does not strike me as being particularly efficient, as obviously there are a great many different file types that could be used for a website. Is there a case statement I could utilize instead?

1
  • any file could be on a website
    – Jeff Schaller
    Jan 31, 2017 at 17:05

1 Answer 1

2

Working entirely from your example, you could simplify the code a little

found=$(find . -maxdepth 1 -type f \( -name '*.htm -o -name '*.html' -o -name '*.php' \) | wc -l)
if [[ $found -gt 0 ]]
then
    echo Possibly a website
fi

Or

found=$(ls -d *.htm *.html *.php 2>/dev/null)
if [[ -n "$found" ]]
then
    echo Possibly a website
fi

Or even

echo Possibly a website
2
  • There we go. Exactly what I am looking for. I did not know that ls by itself was capable of that. Thank you
    – Kevin
    Jan 31, 2017 at 14:56
  • 2
    @Kevin ls isn't doing anything special. The shell expands the wildcards where possible and passes the resulting list to ls.
    – roaima
    Jan 31, 2017 at 14:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .