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"
else
echo "**** No files found ****"
fi
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)
Check more discussion of this question.
Related posts:
Leave a comment
Recent Posts
Tags
active-directory
amazon-ec2
apache
apache2
backup
bash
centos
cisco
command-line
debian
dns
email
exchange
firewall
iis
iis7
iptables
linux
macosx
monitoring
mysql
networking
nginx
performance
permissions
php
postfix
raid
security
sql-server
sql-server-2005
sql-server-2008
ssh
ssl
ubuntu
unix
virtualization
vpn
webserver
windows
windows-7
windows-server-2003
windows-server-2008
windows-server-2008-r2
windows-xp





