Script to count number of files in directories

Hi All!

I would like to have a script that will count the number of files at the top of the hour of soome directories and mail the results to me.
I was thinking on :

a=`/directory/subdirectory/ | wc -l`
echo "/directory/subdirectory :$a"
b=`/another_dir/subdir/ | wc -l`
echo "/another_dir/subdir :$b"

I need to have a

while

, and I would like to have this results every hour of everyday.

Instead of a while, can you use cron? It is usually more efficient than having a resident process sleeping.

#!/bin/ksh
# usage.shl
for dir in /directory1/subdir directory2/subdir
do
echo "$dir $( du $dir |wc -l)" 
done > ./message
mailx -s 'dir usage' me@mycompany.com < ./message

Using crontab

# five minutes after every hour
5 * * * * *  /path/to/usage.shl 2 > /path/to/usage.log 

no crontab

#!/bin/ksh
# usage.shl

while :
do
 for dir in /directory1/subdir directory2/subdir
 do
   echo "$dir $( du $dir |wc -l)" 
 done > ./message
 mailx -s 'dir usage' me@mycompany.com < ./message
 sleep 3600
done

This execution time will drift over time because sleep is not guranteed to "wake up" precisely because of system load.

Hi

Thanks for your quick reply, but your script is giving me the following results:

 /directory1/subdirectory $( du
 /directory2/subdirectory  |wc -l)
  

Did you include

#!/bin/ksh

as the very first line? - I would guess "no". That will fix the problem.

or you can change

echo "$dir $( du $dir |wc -l)" 

to

echo "$dir `du $dir |wc -l`" 

-- those are backticks
If you cannot do that for some reason then:
You are running what shell on what UNIX os?

Hi

I have used

#!/bin/bash

do I have to use korn shell?

---------- Post updated 13-11-12 at 08:30 AM ---------- Previous update was 12-11-12 at 05:28 PM ----------

Hi

I am running bash shell on solaris 10

 echo $SHELL
/bin/bash

---------- Post updated at 03:16 PM ---------- Previous update was at 08:30 AM ----------

any suggestions please:

You can use bash. But those errors are caused by the shell being /bin/sh - a Bourne shell

When crond runs a job in Solaris, by default, it starts the job with /bin/sh. If you do not change that behavior you have to code in Bourne shell. Since you got that error you cannot have copied the original code with the shebang I gave you. The original invoked ksh. use

#!/bin/bash

if you want.

Hi

its not working as it should be, because its giving me single digit values, but in reality those directory have more that 2 digit figures

Please Paste EXACTLY what your script is. Change nothing, please.

I have ommited directory names:

 more usage.sh
#!/bin/ksh
for dir in /directory1/subdirectory /directory2/subdirectory
/directory3/subdirectory 
do
#echo "$dir $( du $dir |wc -l)"
echo "$dir `du $dir |wc -l`"
done > ./message1
mailx -s "n� files" fretagi@mcel.co.mz < ./message1
#!/bin/ksh
for dir in /directory1/subdirectory /directory2/subdirectory
/directory3/subdirectory 
do
#echo "$dir $( find $dir -type f |wc -l)"
  echo "$dir find $dir -type f |wc -l"
done > ./message1
mailx -s "n� files" fretagi@mcel.co.mz < ./message1

Try the change in red.

Hi

In front of the directory name print this:

-type f |wc -l

so no change

You need command substitution like $(...). Try

  echo "$dir $(find $dir -type f |wc -l)" 
1 Like

Thank you very much, now its working