Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

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 and I want to display different messages if a file exists or not. I have a script like:

count=ls /import/*.zip | wc -l

echo "Number of files: " $count
if [ "$count" > "0" ]; then
    echo "Import $count files"
    echo "**** No files found ****"

However, if no files exist, this is showing No such file or directory instead of 0 files. There is a directory in the /import/ directory, so I can't just do a ls command as that will always return a value greater than 0.

How can I count the number of files of a specific extension (.zip, .gz, etc.) and use that value in a bash script to both display the number of files and then use it in an if statement to display different messages?

share|improve this question
up vote 12 down vote accepted

count=$(find /import -maxdepth 1 -name '*.zip' | wc -l)

share|improve this answer
I added -maxdepth 1 to only count files in the current directory – Wallace Sean Jun 7 '11 at 17:23
You probably want to quote '*.zip' in case there are any .zip files in the current directory when this is invoked, in which case the shell will have expanded the *.zip for you, while you want it passed through to find(1). – Phil P Jun 7 '11 at 17:50
Thanks, that's a good catch. – Chris Ting Jun 7 '11 at 18:05

Try with this:

count=$(find /import/ -maxdepth 1 -type f -name '*.zip' | wc -l)
if [ $count -gt 0 ] ; then

The idea is to hide the "no such file" error that gets printed to STDERR by sending it to the bitbucket, and to use the proper test function for comparing numbers. (-gt stands for "greater than". There's also -eq, -lt, -ge, etc.)

share|improve this answer
As an alternative, you could also do count=$(find /import -type f -name "*.zip" | wc -l) – slillibri Jun 7 '11 at 17:12
sure, but that has a different meaning to just ls /import/*.zip if there are subdirectories – Mat Jun 7 '11 at 17:14
I set the maxdepth to avoid counting files in subdirectories and updated the script to use -gt. Thanks for the help – Wallace Sean Jun 7 '11 at 17:27
This fails if there are too many files in the directory to fit in argv for the ls invocation. – Phil P Jun 7 '11 at 17:49
Ah, right. Corrected with a find variant. – Mat Jun 7 '11 at 17:55

I tried your script and, as expected, when no files are matched it does show 0 files in the output, but also shows the ls error just before. Solving this is quite easy, there's no need to ditch ls. Just replace the first line with this:

count=$(ls /import/*.zip 2>/dev/null| wc -l)

2>/dev/null redirects stderr to /dev/null, suppressing the error message.

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.