Script to count files on daily basis

Hi all

I need to count files on a daily basis and send the output via email, what I am currently doing its simply running the following:

ls -lrt *.cdr | awk '{if(($6 == "Jul") && ($7 == "13")) print $0}' | wc -l

, which in this case it will count all files with extension "cdr" from the month of july, today the 13th of july.
I also use the same code to count files with extension "tap"

find  -maxdepth 1 -name "*.cdr" -type f -mtime -1 | wc -l
ls -al --time-style=+%Y-%m-%d *.cdr | grep "2015-07-14" | wc -l

Did you consider using the find utility with all its options and tests?
And, you could use awk to distinguish file extensions and do the counting per extension for you.

the

find

example posted earlier is giving me a different output of my

ls -lrt *.cdr

command

Assuming that you are looking for files dated yesterday (and not July 13 on any earlier years), you need to verify that there is a : in field 8. You could try something like:

ls -l | awk '
$6 != "Jul" || $7 != 13 || !($8 ~ /:/) { next }
/[.]cdr$/ { c++ }
/[.]tap$/ { t++ }
END { printf(".cdr: %d\n.tap: %d\n", c, t) }' 

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk .

1 Like

how do I send that output to an email address, like what would be the code to put in before

| mailx -s "yesterdays�s count" name@domain.com

---------- Post updated at 01:38 PM ---------- Previous update was at 12:39 PM ----------

Hi Don!

Sorry to disturb you, how mail that output using the mailx command

---------- Post updated at 02:24 PM ---------- Previous update was at 01:38 PM ----------

I have used the following code,:

END { printf(".cdr: %d\n.tap: %d\n", c, t) }' > file.tmp
if [ -s file.tmp ]
then
        mailx -s "count" name@domain.com <file.tmp
fi
rm file.tmp

but I am having the following error:
./veri[2]: syntax error at line 7 : `(' unexpected

is END the 7th line of script?

yes:

#!/bin/ksh
ls -l | awk '
$6 != "Jul" || $7 != 13 || !($8 ~ /:/) { next }
/[.]cdr$/ { c++ }
/[.]tap$/ { t++ }
#END { printf(".cdr: %d\n.tap: %d\n", c, t) }'
END { printf(".cdr: %d\n.tap: %d\n", c, t) }' > file.tmp
if [ -s file.tmp ]
then
        mailx -s "count" name@domain.com <file.tmp
fi
rm file.tmp

Remove the ' in the commented out line.

2 Likes

did work out nicely, but how can I put some text just to notify the recipient that the count if of previous day

Use "command substitution" of date in the mail 's subject line.

1 Like

Bu my intention with the script is actually to put this file on a crontab, so that daily it would count and sent an email, but it looks like the way it is, it will only see the month of "july". Am I correct in saying this?

My impression is that this is what you specified.

1 Like

Thank you very much for your help