Operating System - HP-UX
1753866 Members
7584 Online
108809 Solutions
New Discussion

script to count files on daily basis

 
SOLVED
Go to solution
NDO
Super Advisor

script to count files on daily basis

Hello!

 

I am trying to write a script that will count files with *.cdr and *.tap extensions and will sent it to me via email.

At this point I have try the following:

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

but that has a problem that will only give me 11th of july files, so I will have to edit the script everyday....

18 REPLIES 18
Bill Hassell
Honored Contributor

Re: script to count files on daily basis

I think there is an additional requirement that you did not mention.

This will count files with *.cdr and *.tap extensions:

ls -1 *\.cdr *\.tap | wc -l

Did you leave out a requirement that the test only counts files created on today's date?

 

If that is the real requirement, using ls -l very cumbersome.

Use find, something like this:

 

find . \( -name *.cdr -o -name *.tap \) -mtime -1 | wc -l

-mtime -1 shows files that were created or modified in the last 24 hours.

 



Bill Hassell, sysadmin
NDO
Super Advisor

Re: script to count files on daily basis

the idea is to run the script after the midnight (put on crontab) to count those files of the previuos day.

Your

find

example returned :

find . \( -name *.cdr -o -name *.tap \) -mtime -1 | wc -l
find: bad option ICTEMSC32015061614501134843.cdr
0

as an example:

ls -lrt *.cdr | awk '{if(($6 == "Jul") && ($7 == "14")) print $0}' | wc -l
1570 
Patrick Wallek
Honored Contributor

Re: script to count files on daily basis

You need quotes around the name specifications, like so:

 

find . \( -name "*.cdr" -o -name "*.tap" \) -mtime -1 | wc -l
NDO
Super Advisor

Re: script to count files on daily basis

its actually giving me a total:

find . \( -name "*.cdr" -o -name "*.tap" \) -mtime -1 | wc -l
1789

But what I really want is an output like this:

cdr: 1197
tap: 229
Patrick Wallek
Honored Contributor

Re: script to count files on daily basis

Try this:

 

CDRCOUNT=$(find . -name "*.cdr" -mtime -1 | wc -l)
TAPCOUNT=$(find . -name "*.tap" -mtime -1 | wc -l)
echo "cdr: ${CDRCOUNT}"
echo "tap: ${TAPCOUNT}"

 

 

NDO
Super Advisor

Re: script to count files on daily basis

Patrick:

 

I did run your script which gave the following output:

 

cdr: 1588
tap: 224

But if I just do :

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

and :

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

I have different values... 

Patrick Wallek
Honored Contributor

Re: script to count files on daily basis

The "find" with the '-mtime -1' will shows files created within the last 24 hours.  It will NOT just show files created yesterday.

 

If the find command is run at midnight, then the count should be pretty accurate.  However, if you want to be able to specify the date to find the files for, then that will take some more work...

Patrick Wallek
Honored Contributor

Re: script to count files on daily basis

OK, try this.  This assumes you have 'perl' in your path.  If you don't find where the perl executable is and add the full path to the perl commands.

 

#!/usr/bin/sh

# Get yesterdays date
YESTMON=$(perl -MPOSIX -le '@t=localtime;--$t[3];print strftime "%Y %b %d",@t' | awk '{print $2}')
YESTDAY=$(perl -MPOSIX -le '@t=localtime;--$t[3];print strftime "%Y %b %d",@t' | awk '{print $3}')

CDRCOUNT=$(ls -lrt *.cdr | awk '{if (($6 == "'${YESTMON}'") && ($7 == "'${YESTDAY}'")) print $0}' | wc -l)
TAPCOUNT=$(ls -lrt *.tap | awk '{if (($6 == "'${YESTMON}'") && ($7 == "'${YESTDAY}'")) print $0}' | wc -l)

echo "cdr: ${CDRCOUNT}"
echo "tap: ${TAPCOUNT}"

Here is an ls of my working directory:

 

# ls -l
total 80
drwxr-xr-x 2 root sys 8192 Jul 15 10:31 .
drwxrwxrwt 16 root root 8192 Jul 15 10:42 ..
-rw-r--r-- 1 root sys 0 Jul 14 10:00 file1.cdr
-rw-r--r-- 1 root sys 0 Jul 14 10:00 file2.cdr
-rw-r--r-- 1 root sys 0 Jul 14 10:00 file3.cdr
-rw-r--r-- 1 root sys 0 Jul 14 10:00 file3.tap
-rw-r--r-- 1 root sys 0 Jul 14 10:00 file4.tap
-rw-r--r-- 1 root sys 0 Jul 14 10:00 file5.tap
-rw-r--r-- 1 root sys 0 Jul 14 10:00 file6.tap
-rwx------ 1 root sys 1790 Apr 8 14:22 generate_cfg2html-int.sh
-rwx------ 1 root sys 1794 Apr 8 14:02 generate_cfg2html.sh
-rwxr--r-- 1 root sys 499 Jul 15 10:42 pwtest.sh

 

Here is the script running:

# ./pwtest.sh
cdr: 3
tap: 4

Dennis Handly
Acclaimed Contributor

Re: script to count files on daily basis

>ls -1 *\.cdr *\.tap | wc -l

 

There is no need to use "\." for filename generation, a period isn't special like for REs.

 

>Did you leave out a requirement that the test only counts files created on today's date?

 

That requirement can't be satisfied, only "modified" today.

 

>what I really want is an output like this:

 

If you have zillions of files, it is better to use find to get both and then separate the files after.

 

>find with the '-mtime -1' will shows files created within the last 24 hours.

 

Actually modified.

 

>This assumes you have 'perl' in your path.

 

If you have perl, you can do all the work there.  :-)

Otherwise have perl return two timestamps and create two reference files and pass them into find(1).