PerlHaven2k
PerlHaven2k

Reputation: 21

UNIX Count Script

I need a UNIX Kornshell (ksh) script that counts the number of files in directory. If the files exceed 20 files, then email results. I want the script to run every hour. I do not have access to cron. I am somewhat new to UNIX. Windows guy all my career. This is what I have so far..

#!/bin/ksh
# count.sh

while :
do
 for dir in /directory1/
 do
  echo "$dir `du $dir |wc -l`" 
 done > ./message
 mailx -s 'Dir Count' [email protected] < ./message
 sleep 3600
done

Any help is greatly appreciated.

Upvotes: 1

Views: 433

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 754490

The du command as shown will produce one line per directory inside the target directory, /directory1.

Your question isn't clear. It says 'if the files exceed 20 files', which I interpret as meaning 'if the number of files exceeds 20'. You're not dealing with files (as opposed to directories, FIFOs, and other types), and you don't test for "more than 20". I'm going to simplify things, and assume you mean 'if the number of names in the directory exceeds 20'.

dir=/directory1/
while :
do
    names=$(ls "$dir" | wc -l)
    if [ "$names" -gt 20 ]
    then echo "$dir $names" | mailx -s 'Dir Count' [email protected]
    fi
    sleep 3600
done

Upvotes: 1

Related Questions