Aug 9, 2011

Bash script to count number of files


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 -lecho "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?


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

Related posts:

  1. Monitoring script in bash for perl script fails
  2. Bash script normal user to Root
  3. Batch script to download many files via http?
  4. Too complex password for a FTP bash script?
  5. How to set a timeout within a BASH script for a process that sometimes becomes inactive?

Leave a comment