Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a script which will download html files into current directory.
Then it'll generate a report based on these html files.
At last, it'll delete all these html files.
So, when I run this script, I want to make sure there is no html files in current dir.

This is what I got:

if ls *.html >/dev/null 2>&1; then
    echo 'clear HTML files first'

Is there any easy way to check?

share|improve this question
Your example should work if you want no folders, symbolic links, etc. named *.html either. – Daniel Beck Apr 8 '12 at 15:38
Why not creating a temporary directory with mktemp -d ? – jfgagne Apr 8 '12 at 17:47
up vote 1 down vote accepted

Using the failglob option; checks for any matches:

if (shopt -s failglob; true *.html) 2>/dev/null; then
    echo "Found files"

Using nullglob and arrays; counts the files:

numfiles=$( shopt -s nullglob; files=(*.html); echo ${#files[@]} )

if (( numfiles > 0 )); then
    echo "Have $numfiles HTML files"
    echo "No files"

Another way, without using nullglob or discarding $files:

if [[ -e ${files[0]} ]]; then
    echo "$numfiles files found"

Using nullglob for the entire script – might be okay in some cases, bad in others:

#!/usr/bin/env bash
shopt -s nullglob


share|improve this answer

Count the number of HTML files (not folders, symbolic links, etc.) in the current directory, and, if greater than zero, abort:

if [[ $( find . -maxdepth 1 -type f -name "*.html" | wc -l ) -gt 0 ]] ; then
    echo "Oh no, there are HTML files!" >&2
    exit 1
share|improve this answer
While newlines in file names will break the counter, they only increase it. Any match will increase the counter beyond 0. – Daniel Beck Apr 8 '12 at 15:40
for i in *.html
  if [ -f "$i" ] 
     echo "Cannot run, html files in current directory"
     exit 1
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.