Question: > 
> **Possible Duplicate:**  
> 
> [Checking if a directory contains files](https://stackoverflow.com/questions/91368/checking-if-a-directory-contains-files) 
> 
> 
> 

I want to see if a directory is empty, so I am using `[` as follows:

```
[ -f "./ini/*"]

```

And the return value is always 1, even though the directory is not empty.
Any ideas what's wrong?

Answer: 
Try this:

```
$ echo "*"   #With quotes

```

vs.

```
$ echo *    #No quotes

```

See the difference? When you put quotes around a string, the shell can't *glob* to replace the asterisk with the matching files.

There are multitude of ways to get what you want. Simplest is to use the `ls` command without the test command. The `if` statement takes the output results of the ***command*** you give it. The `[...]` simply are an alias to the `test` command. These two statements are equivalent:

```
$ if test -f foo.txt

$ if [ -f foo.txt ]

```

Basically, all the test command does is return a `0` if the test is true or a non-zero if the test is false. All the `if` command does is execute the `if` statement if the command it's given returns a `0` exit code. The reason I'm going all through this is to say let's forget the test and simply use the `ls` command.

Assume that you have a file `foo.txt`, but no file `bar.txt`. Try the following:

```
$ ls foo.txt > /dev/null 2>&1
$ echo $?
0
$ ls bar.txt > /dev/null 2>&1
$ echo $?
2

```

The `> /dev/null 2>&1` is used to inhibit all output. Notice that the exit code of the `ls` command when a file exists is `0` while the exit code of the `ls` command when the file doesn't exist isn't `0`. (It's `2` in this case). Now, let's use that instead of the test command:

```
if ls ./ini/* > /dev/null 2>&1
then
    echo "There are files in that directory!"
else
    echo "There are no files in that directory"
fi

```

Notice I don't even bother with the test command (which can be the word `test` or the `[..]`). 

Hope this makes sense to you. It's sometimes difficult for people to realize that in BASH shell, the `if` command and the `[...]` are two separate commands, and all `if` is doing is running the command it's given and then acting depending upon the exit code of the command it runs.
Beware of filenames starting with '.', normal globbing or using `ls` (*shudder*) will not detect those There are several ways to do this, the simplest I can come up with is:

```
/home/user1> mkdir ini
/home/user1> cd ini
/home/user1/ini> touch .fred jim tom dick harry
/home/user1/ini> cd ..
/home/user1> declare -a names
/home/user1> names=(./ini/* ./ini/.*)
/home/user1> number_of_files=$(( ${#names[@]} -2 ))
/home/user1> echo $number_of_files
5

```

The `-2` is required to remove `.` and `..` from the list.