Shell script to compare two files of todays date and yesterday's date

hi all,
How to compare two files whether they are same are not...? like i had my input files as 20141201_file.txt and 20141130_file2.txt

how to compare the above files based on date .. like todays file and yesterdays file...?

One way:

 
#!/bin/bash
 
file1=$(date '+%Y%m%d')
file2=$(date -d "1 day ago" '+%Y%m%d')
file1=$file1"_file.txt"
file2=$file2"_file.txt"
diff $file1 $file2
 

Hello hemanthsaikumar,

If you are saying files are of same date or not we can use following, if you need to compare data of these files then
kindly let us know some input data on same so that we can help more.

cat compare_fles.ksh
A=$1
B=$2
awk -vC=`echo $A` -vD=`echo $B` 'BEGIN{gsub(/\_.*/,X,C);gsub(/\_.*/,X,D);if(C == D){print "Files are of same date."} else {print "Files are NOT same date."}}'

After running script we will get following output.

./compare_fles.ksh 20141201_file.txt 20141130_file2.txt

Output will be as follows.

Files are NOT same date.

Thanks,
R. Singh

hi all,,
thanks for the reply... but i need to pass the date in the parameter so that it should compare with the previous date file ... for example

if my date is 20141201_file.txt then it should compare with 20141130_file.txt ,the date in the file name is getting generated through the parameter iam passing ..iam passing the date parameter in the below format 2014-12-01 to generate the file with name 20141201_file.txt

Hello hemanthsaikumar,

Following may help you in same.

DATE1=$1
NAME1=`echo $DATE1 | awk '{gsub(/\-/,X,$0);print $0}'`
VALUE=`find -type f -name "$NAME1""_file.txt"`
if [[ -n $VALUE ]]
then
        echo "File named" "$NAME1""_file.txt" "has been found."
else
        echo "File named" "$NAME1""_file.txt" "has NOT been found."
fi

Run the script as follows.

./check_date12.ksh 2014-11-30

Output will be as follows.

File named 20141130_file.txt has been found.

Another example where file is not found in directory.

./check_date12.ksh 2014-15-30

Output will be as follows.

File named 20141530_file.txt has NOT been found.

Hope this helps.

EDIT: Sorry I didn't understand requirement before following may help.

DATE=`date`
DATE1=`date -d"1 day ago"`
value1=`find -type f -name"$DATE""file.txt"`
value=`find -type f -name"$DATE1""file.txt"`
if [[ -n $value && -n value1 ]]
then
 diff $value $value1
else
 echo "Either file named " "$value1" " OR file named " "$value" " has NOT been found."
fi

Thanks,
R. Singh