Find the answer to your Linux question:
Results 1 to 10 of 10
I need help making a shell script to check if a file has changed within a set period of time....
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2003
    Posts
    3

    shell script commands to check if a file has changed


    I need help making a shell script to check if a file has changed within a set period of time.

  2. #2
    Linux Engineer
    Join Date
    Apr 2003
    Location
    Sweden
    Posts
    796
    Hello

    You can save an md5sum key of the file which is like a fingerprint and try to match them later after some time or if you can save the old file. you can diff the old one against the new one. Here is a little sample with md5sum. Replace filetocheck to the filename that you want to check and the correct locations. If you want a better program but can´t program your self or just dont think its fun, order it on my page http://www.utterberg.com and then under forums and programming ordering, and i will fix it for you.

    #!/bin/sh
    #
    #
    # MD5FILE-parameter specifies where we want to save our md5print for
    # later use.
    MD5FILE=/tmp/.md5savefile

    # The FILE_TO_CHECK-parameter specifies the file we want to monitor
    # changes
    FILE_TO_CHECK=/tmp/filetocheck

    if [ ! -f $FILE_TO_CHECK ]
    then
    echo "ERROR Couldnt locate file to check:$FILE_TO_CHECK"
    exit 1
    fi

    echo "Taking a print on $FILE_TO_CHECK with md5sum"
    MD5PRINT=`md5sum $FILE_TO_CHECK | cut -d " " -f2`

    if [ -z $MD5PRINT ]
    then
    echo "ERROR Recived an empty MD5PRINT thats not valid, aborting"
    exit 1
    else
    echo "MD5PRINT we got was:$MD5PRINT"
    fi

    if [ -f $MD5FILE ]
    then
    echo "Found an old savefile:$MD5FILE we trying to match prints"
    OLDMD5PRINT=`cat $MD5FILE`

    if [ -z $OLDMD5PRINT ]
    then
    echo "Got an empty string from the oldfile, aborting"
    exit 1
    if

    if [ "$OLDMD5PRINT" = "$MD5PRINT" ]
    then
    echo "New and old md5print are identical, the file hasnt been changed"
    else
    echo "WARNING the old and new md5print doesnt match, the file has been changed"
    fi

    fi

    echo "Saving to new md5print in logfile:$MD5FILE for later checks"
    echo $MD5PRINT > $MD5FILE

    if [ $? = 0 ]
    then
    echo "Wrote to file OK"
    else
    echo "Writing to file failed...why??"
    exit 1
    fi


    Regards

    Regards

    Andutt

  3. #3
    Just Joined!
    Join Date
    Apr 2003
    Posts
    3
    Thanks

  4. #4
    Just Joined!
    Join Date
    Apr 2003
    Posts
    3
    I keep getting Unexpected end of file....

    not the first time I have seen this error when making scripts but I still have no idea what it means.

  5. #5
    Linux Engineer
    Join Date
    Apr 2003
    Location
    Sweden
    Posts
    796
    OK

    Probobly have i missed some if statement somewhere, try to execute the script with sh -x <scriptname> and see where its wrong,

    I found a syntax error, ive fixed it now, try to use this script, but if you get an error again try to execute it with the sh -x flag. Here it comes again..

    #!/bin/sh
    #
    #
    # MD5FILE-parameter specifies where we want to save our md5print for
    # later use.
    MD5FILE=/tmp/.md5savefile

    # The FILE_TO_CHECK-parameter specifies the file we want to monitor
    # changes
    FILE_TO_CHECK=/tmp/filetocheck

    if [ ! -f $FILE_TO_CHECK ]
    then
    echo "ERROR Couldnt locate file to check:$FILE_TO_CHECK"
    exit 1
    fi

    echo "Taking a print on $FILE_TO_CHECK with md5sum"
    MD5PRINT=`md5sum $FILE_TO_CHECK | cut -d " " -f2`

    if [ -z $MD5PRINT ]
    then
    echo "ERROR Recived an empty MD5PRINT thats not valid, aborting"
    exit 1
    else
    echo "MD5PRINT we got was:$MD5PRINT"
    fi

    if [ -f $MD5FILE ]
    then
    echo "Found an old savefile:$MD5FILE we trying to match prints"
    OLDMD5PRINT=`cat $MD5FILE`

    if [ -z $OLDMD5PRINT ]
    then
    echo "Got an empty string from the oldfile, aborting"
    exit 1
    fi

    if [ "$OLDMD5PRINT" = "$MD5PRINT" ]
    then
    echo "New and old md5print are identical, the file hasnt been changed"
    else
    echo "WARNING the old and new md5print doesnt match, the file has been changed"
    fi

    fi

    echo "Saving to new md5print in logfile:$MD5FILE for later checks"
    echo $MD5PRINT > $MD5FILE

    if [ $? = 0 ]
    then
    echo "Wrote to file OK"
    else
    echo "Writing to file failed...why??"
    exit 1
    fi


    Regards

    Regards

    Andutt

  6. #6
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    If you just want to know when the file was last changed, just check the file's mtime with ls -l. The time printed in its output is when the file was last changed. You can also add "ls -l file" (where file should, of course, be replaced with the actual filename), to your crontab so that it's checked once in a while.

  7. #7
    Linux Engineer
    Join Date
    Apr 2003
    Location
    Sweden
    Posts
    796
    Hello NathanGraham

    Ive put out a program for you now, that i have verified on my linux, and it workes fine. You can get it on http://www.utterberg.com under the download section. I called it filechecker.

    Make sure that the executeble flag is set otherwise set it witch cmod +x or chmod 650.

    Regards

    Regards

    Andutt

  8. #8
    Just Joined!
    Join Date
    Apr 2006
    Posts
    6

    file checker

    Hi andutt;

    I am looking the file checker program, but can not download it from the link www.utterberg.com. Do you still have it ?

    I am tring to use ls -l and compare the time stamp.
    How do I compare two time in shell script.
    I try like this if time1 != time2
    then echo "xxxxxxx"
    fi

    but that does not work.

    Please help
    thanks

    anbri

  9. #9
    Just Joined!
    Join Date
    Sep 2004
    Location
    India
    Posts
    3
    Why dont u simply do


    #stat filename

    It will give something like ---
    Access: 2004-05-08 02:50:41.000000000 -0700
    Modify: 2006-05-08 02:40:26.000000000 -0700
    Change: 2006-05-08 02:40:26.000000000 -0700


    And compare Access/Modify

    Thanks,
    Avirup dasgupta

  10. #10
    Just Joined!
    Join Date
    Apr 2006
    Posts
    6

    Filechecker

    Thanks for suggestion. I will try it .

    Anbri

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •