Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script that will count and then mail

Status
Not open for further replies.

BrianC25

MIS
Jun 14, 2001
26
US
I am new at scripting and I just need a script that will count the number of files in a directory and if the number of files in the directory are greater than a certian number, have it send a mail to myself.

If any one can help it would be greatly appreciated. Thanks!

Brian
 
two quick ways

#!/bin/sh
DIRECTORYINQUESTION=$1
NOOFLINES=`ls $DIRECTORYINQUESTION/* | wc -l`
if [ $NOOFLINES -gt $MAGICNUMBER ] ; then
echo "$DIRECTORYINQUESTION has $NOOFLINES files in it" | mail myself@mailhost.domain
fi

#!/bin/sh
DIRECTORYINQUESTION=`pwd`
NOOFLINES=`find $DIRECTORYINQUESTION -level 0 -print| wc -l`
if [ $NOOFLINES -gt $MAGICNUMBER ] ; then
echo "$DIRECTORYINQUESTION has $NOOFLINES files in it" | mail myself@mailhost.domain
fi


I suggest the second version because if you are worried about the number of files in a directory then its probably because you have too many of them and ls will error out with too many files. (you may also have to change level to depth depending on your *nix.

stan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top