LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   File Modification Date Comparison (https://www.linuxquestions.org/questions/programming-9/file-modification-date-comparison-140020/)

MadTurki 01-29-2004 11:44 AM

File Modification Date Comparison
 
I'm not a programmer by trade but have a decent understanding and ability to follow code. I'm using RedHat 9 and wanted to be able to check the modification date and compare it to the current date to insure that backups have been completed properly. ie a directory is tarred and gzipped and then put into a backups directory. Then I want to go through each file sequentially in the backups directory and make sure each was created or modified on the current date. Hope this makes sense! I'd also like to do with with bash scripting because it'll be easiest for me to understand and I think implement. Thanks!

jim mcnamara 01-29-2004 11:48 AM

find -mtime will list files older (or newer) than a certain time.

check the man page for find.

MadTurki 01-29-2004 11:52 AM

Yeah. I was looking at that. I guess that would help me to find out if ANY of the files failed. But I'd like to be able to adapt whatever I get here to identify the file and the re-attempt the backup. Though, I guess I could use some sort of string monipulation to isolate the names of the files from what's returned... But I think I'd get stuck again after that anyways! And it seems a bit like a hack job to have to search through a string like that :S

I know that 'date -r $filename + %e' will return the date. Which I can then compare to 'date +%e'. But that only goes for one file at a time :S How can I get $filename to be replaced sequentially with each file in the directory?

jim mcnamara 01-29-2004 03:13 PM

Code:

for filename in /directory1/subdirectory/*
do
    # play with $filename
done


MadTurki 03-08-2004 11:23 AM

I cant get this to work right. Right now I have (I know this is wrong btw and I've tried variations):

for filename in /home/whatever/*; do
if [ date -r $filename +%e ] = [ date +%e ]; then
echo "success"
else
echo "failure"
fi
done

This is returning the error:
./dateCheck.sh: line 2: [: missing ']'

I've tried other things with quotes and still gotten errors. What I'm trying to do is examine the modification date of all the files in a directory one at a time, and compare them with the current date. If they're the same, I want a success message printed and if different then an error message. Thanks for your help

jim mcnamara 03-08-2004 01:46 PM

syntax problem:
Code:

a=`date -r $filename +%e`
now=`date +%e`
if [ "$a" = "$now" ]; then
  # do stuff
fi


MadTurki 03-08-2004 02:00 PM

Beeeeeaaauty! Thank you so much. Works perfectly :)

Actually - It doesnt... They all get reported as failed even if the dates should match! I did an "echo $a" and an "echo $now" during the run and it only out put "date -r $filename +%e" and "date +%e" respectively. Not quite what I was hoping! Why isnt it processing that before it's put into the variable?

MadTurki 03-08-2004 04:02 PM

Sorry - now what if I wanted to make this search the subdirectory which corresponds to the current day?

current='date +%a'
cd $current

a=`date -r $filename +%e`
now=`date +%e`
if [ "$a" = "$now" ]; then
# do stuff
fi

That way no matter when I run this script it'll go into the right directory and check the files. Thanks for your help. I'm sure this is just another simple problem but I cant find a solution in my book.


All times are GMT -5. The time now is 03:49 AM.