Count files between multiple directories

Hi All,

Everyday we will receive 33 files in our source directory(/prd/pk) with the current date. Once our jobs are completed all the 33 files immediately will be moved to backup folder (/prd/pk/backup).

Now, I need to check between source file directory (/prd/pdk) and backup file directory based on date to ensure that all files are received for current date.

If not, I need to display how may files are missed for current date.

Kindly help me how to achive this.

 
Ex:
 
Source File Directory:
/prd/pdw
Fileone_20140627
FileTwo_20140627
FileThree_20140627
 
Backup File Directory:
/prd/pdw/Backup
 
Fileone_20140626
FileTwo_20140626
FileThree_20140626
FileFour_20140626
FileFive_20140626
 
Expected Output:
 
FileFour_20140626
FileFive_20140626
 

if you are on a linux system this will definetly work in bash, for unix you will need to test in ksh

#!/bin/bash

Date=`date "+%Y%m%d"`

for fl in /prd/pdw/Backup/*${Date}*
do
    file=`basename $fl`;
    if [ ! -e "/prd/pdw/${file}" ]
    then
        echo "$file"
    fi
done

It seems more like this would be quicker and give the answer required:-

#!/bin/ksh
Date=`date "+%Y%m%d"`
Req_count=33

cd /prd/pdw
count=`ls -1d *${Date}*|wc -l`

if [ $count -ne $Req_count ]
then
   ((Shortfall=$Req_count-$count))
   echo "You have only got $count files.  You are $Shortfall short."
   exit 99
fi

Set the value in red as applicable if your needs change.

Finding no files will give you an error message to standard error from the ls and the count will be zero. You could remove the message by changing that line to have:-

count=`ls -1d *${Date}* 2>/dev/null|wc -l`

I hope that this helps,
Robin

Hello,

I created these files for testing purpose,

kibou@laptop:~$ ls dirToday/
file1_2014-06-28  file2_2014-06-28
kibou@laptop:~$ ls dirBackup/
file1_2014-06-27  file2_2014-06-27  file3_2014-06-27  file4_2014-06-27

So I've tried something like this,

kibou@laptop:~$ ls dirToday/ | awk -F"_" '{print $1}' > today.lst
kibou@laptop:~$ ls dirBackup/ | grep -f today.lst 
kibou@laptop:~$ echo "* Missing files *"; ls dirBackup/ | grep -vf today.lst 
* Missing files *
file3_2014-06-27
file4_2014-06-27