Amir Amir -4 years ago 132
Bash Question

Unix Count Multiple Folders Needed

I have a directory on unix server.

cd /home/client/files


It has multiple client folders like these below.

cd /home/client/files/ibm
cd /home/client/files/aol
cd /home/client/files/citi


All of them send us a file starting with either lower or upper case like below:

pre-ibm-03222017
PRE-aol-170322


Once we recieve the files, we process them and convert pre to pro as below:

pro-ibm-03222017
PRO-aol-170322


I want to count the files processed each day. Here is what I am looking for:

If I can just get the total count per client, that would be perfect. If not, then the total count overall.

Keep in mind it has all files as below:

cd /home/client/files/ibm
pre-ibm-03222017
pro-ibm-03222017

cd /home/client/files/aol
PRE-aol-170322
PRO-aol-170322


And I want to COUNT ONLY the PRO/pro that will either be lower or upper case. One folder can get more than 1 file per day.

I am using the below command:

find /home/client/files -type f -mtime -1 -exec ls -1 {} \;| wc -l


But it is giving me the total count of pre and pro files and also it is counting files for last 24 hours....and not the current day only.

For Example. It is currently 09:00 PM. The above command include files received yesterday between 09:00 PM and 12:00 AM as well. I don't wan't those. In other words if I run it at 01:00 AM....it should have all files for 1 hour only and not last 24 hours.

Thanks

---- Update -----

This works great for me.

touch -t 201703230000 first
touch -t 201703232359 last
find /home/client/files/ -newer first ! -newer last | grep -i pro | wc -l


Now, I was just wondering if I can pass the above as parameter.

For example, instead of using touch -t date and alias.....I want to type shortcuts and dates only to get the output. I have made the following aliases:

alias reset='touch -t `date +%m%d0000` /tmp/$$'
alias count='find /home/client/files/ -type f -newer /tmp/$$ -exec ls -1 {} \; | grep -i pro | wc -l'


This way as soon as I logon to the server, I type reset and then I type count and I get my daily number.

I was wondering if I can do something similar for any duration of days by setting date1 and date2 as aliases. If not, then perhaps a short script that would ask for parameters.

Answer Source

What about this?

touch -t `date +%m%d0000` /tmp/$$
find /home/client/files -type f -newer /tmp/$$ -exec ls -1 {} \; | grep -i pro | wc -l
rm /tmp/$$

Other options for finding a file created today can be found in this question: How do I find all the files that were created today

Actually, a better way to do this is to just use this:

find /home/client/files -type f -m 0 | grep -i pro | wc -l

You can replace -m 0 with -m 5 to find files 5 days old.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download