Bash script to check if a file is modified or not

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
4
down vote

favorite
2












I try to write script to check if a file was modified or not. If it did, it should echo "Error!", if not - script keeps running.



My script



#!/bin/bash
date=$(stat -c %y)$1
while true
do date2=$(stat -c %y$1)
if (date2 != date)
echo "error!"
done


Are there any errors?










share|improve this question























  • shellcheck will report some warnings, if that's what you're asking about....
    – drewbenn
    Jun 4 '16 at 6:42














up vote
4
down vote

favorite
2












I try to write script to check if a file was modified or not. If it did, it should echo "Error!", if not - script keeps running.



My script



#!/bin/bash
date=$(stat -c %y)$1
while true
do date2=$(stat -c %y$1)
if (date2 != date)
echo "error!"
done


Are there any errors?










share|improve this question























  • shellcheck will report some warnings, if that's what you're asking about....
    – drewbenn
    Jun 4 '16 at 6:42












up vote
4
down vote

favorite
2









up vote
4
down vote

favorite
2






2





I try to write script to check if a file was modified or not. If it did, it should echo "Error!", if not - script keeps running.



My script



#!/bin/bash
date=$(stat -c %y)$1
while true
do date2=$(stat -c %y$1)
if (date2 != date)
echo "error!"
done


Are there any errors?










share|improve this question















I try to write script to check if a file was modified or not. If it did, it should echo "Error!", if not - script keeps running.



My script



#!/bin/bash
date=$(stat -c %y)$1
while true
do date2=$(stat -c %y$1)
if (date2 != date)
echo "error!"
done


Are there any errors?







bash shell-script






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 2 '16 at 14:58









MelBurslan

5,16511433




5,16511433










asked Jun 2 '16 at 14:44









Elisia Light

81125




81125











  • shellcheck will report some warnings, if that's what you're asking about....
    – drewbenn
    Jun 4 '16 at 6:42
















  • shellcheck will report some warnings, if that's what you're asking about....
    – drewbenn
    Jun 4 '16 at 6:42















shellcheck will report some warnings, if that's what you're asking about....
– drewbenn
Jun 4 '16 at 6:42




shellcheck will report some warnings, if that's what you're asking about....
– drewbenn
Jun 4 '16 at 6:42










4 Answers
4






active

oldest

votes

















up vote
13
down vote













you can use inotifywait , read more




inotifywait - wait for changes to files using inotify



inotifywait efficiently waits for changes to files using Linux's
inotify(7) interface. It is suitable for waiting for changes to files
from shell scripts. It can either exit once an event occurs, or
continually execute and output events as they occur.




use this command :



$ inotifywait -m -e modify /tmp/testfile


when i write into testfile , inotifywait alarm to me



e.g;



echo "bh" > /tmp/testfile


inotifywait show this message:



$ inotifywait -m -e modify /tmp/testfile
Setting up watches.
Watches established.
testfile MODIFY
testfile MODIFY


also you can redirect output to while statement :



while read j
do
echo "file changed"
break
done < <(inotifywait -q -e modify /tmp/testfile)





share|improve this answer





























    up vote
    4
    down vote













    filename="$1"

    m1=$(md5sum "$filename")

    while true; do

    # md5sum is computationally expensive, so check only once every 10 seconds
    sleep 10

    m2=$(md5sum "$filename")

    if [ "$m1" != "$m2" ] ; then
    echo "ERROR: File has changed!" >&2
    exit 1
    fi
    done





    share|improve this answer



























      up vote
      0
      down vote













      Consider using md5sum, is safer to check real file modifications. This is script will return "The files are different" if a file diverge from the other, but when you equalize then, it will say the files are equal again.



      #!/bin/bash

      loop1() cut -d' ' -f1)
      if [ "$md5f2" != "$md5f1" ]; then
      echo "The files are different now."
      #stop loop:
      break
      fi
      done


      loop2() cut -d' ' -f1)
      if [ "$md5f2" == "$md5f1" ]; then
      echo "The files are equal again."
      #stop loop:
      break
      fi
      done


      while true; do
      loop1 "$1" "$2"
      loop2 "$1" "$2"
      done


      Save it as autocompare and run like:



      ./autocompare file1 file2 





      share|improve this answer






















      • Not i corrected it 4 mins ago.
        – Luciano Andress Martini
        Jun 2 '16 at 15:07










      • md5sum is the right approach, but that script is far more complicated than it needs to be. and it's comparing two different files, not just one file against possibly different versions of itself.
        – cas
        Jun 3 '16 at 2:05






      • 3




        md5sum might not be a perfect approach if this is a particularly huge file and you plan to monitor this continually (excessive i/o and cpu usage?)
        – steve
        Jun 4 '16 at 16:43

















      up vote
      0
      down vote













      If you do want to 'manually' check for change in the modification timestamp, as opposed to actual difference in the contents, you need:



      • stat -c %y $1 consistently with the separating spaces and inside $( ... ). Even better, stat -c %y "$1" will work if your filename contains whitespace or any 'globbing' character


      • test with classic [ ... ] or test ... and "$var" (because stat %y contains spaces; stat %Y would avoid that) or bash-enhanced [[ ... ]] which doesn't need quotes -- but not ( ... ) which does something completely different namely execute in a subshell


      • some delay between loops so this doesn't completely hog your system


       #!/bin/bash 
      date=$(stat -c %y "$1")
      while sleep 1; do date2=$(stat -c %y "$1")
      if [[ $date2 != $date ]]; then echo "changed!"; break; fi
      # possibly exit [status] instead of break
      # or if you want to watch for another change, date=$date2
      done





      share|improve this answer




















        Your Answer







        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "106"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        convertImagesToLinks: false,
        noModals: false,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













         

        draft saved


        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f287230%2fbash-script-to-check-if-a-file-is-modified-or-not%23new-answer', 'question_page');

        );

        Post as a guest






























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        13
        down vote













        you can use inotifywait , read more




        inotifywait - wait for changes to files using inotify



        inotifywait efficiently waits for changes to files using Linux's
        inotify(7) interface. It is suitable for waiting for changes to files
        from shell scripts. It can either exit once an event occurs, or
        continually execute and output events as they occur.




        use this command :



        $ inotifywait -m -e modify /tmp/testfile


        when i write into testfile , inotifywait alarm to me



        e.g;



        echo "bh" > /tmp/testfile


        inotifywait show this message:



        $ inotifywait -m -e modify /tmp/testfile
        Setting up watches.
        Watches established.
        testfile MODIFY
        testfile MODIFY


        also you can redirect output to while statement :



        while read j
        do
        echo "file changed"
        break
        done < <(inotifywait -q -e modify /tmp/testfile)





        share|improve this answer


























          up vote
          13
          down vote













          you can use inotifywait , read more




          inotifywait - wait for changes to files using inotify



          inotifywait efficiently waits for changes to files using Linux's
          inotify(7) interface. It is suitable for waiting for changes to files
          from shell scripts. It can either exit once an event occurs, or
          continually execute and output events as they occur.




          use this command :



          $ inotifywait -m -e modify /tmp/testfile


          when i write into testfile , inotifywait alarm to me



          e.g;



          echo "bh" > /tmp/testfile


          inotifywait show this message:



          $ inotifywait -m -e modify /tmp/testfile
          Setting up watches.
          Watches established.
          testfile MODIFY
          testfile MODIFY


          also you can redirect output to while statement :



          while read j
          do
          echo "file changed"
          break
          done < <(inotifywait -q -e modify /tmp/testfile)





          share|improve this answer
























            up vote
            13
            down vote










            up vote
            13
            down vote









            you can use inotifywait , read more




            inotifywait - wait for changes to files using inotify



            inotifywait efficiently waits for changes to files using Linux's
            inotify(7) interface. It is suitable for waiting for changes to files
            from shell scripts. It can either exit once an event occurs, or
            continually execute and output events as they occur.




            use this command :



            $ inotifywait -m -e modify /tmp/testfile


            when i write into testfile , inotifywait alarm to me



            e.g;



            echo "bh" > /tmp/testfile


            inotifywait show this message:



            $ inotifywait -m -e modify /tmp/testfile
            Setting up watches.
            Watches established.
            testfile MODIFY
            testfile MODIFY


            also you can redirect output to while statement :



            while read j
            do
            echo "file changed"
            break
            done < <(inotifywait -q -e modify /tmp/testfile)





            share|improve this answer














            you can use inotifywait , read more




            inotifywait - wait for changes to files using inotify



            inotifywait efficiently waits for changes to files using Linux's
            inotify(7) interface. It is suitable for waiting for changes to files
            from shell scripts. It can either exit once an event occurs, or
            continually execute and output events as they occur.




            use this command :



            $ inotifywait -m -e modify /tmp/testfile


            when i write into testfile , inotifywait alarm to me



            e.g;



            echo "bh" > /tmp/testfile


            inotifywait show this message:



            $ inotifywait -m -e modify /tmp/testfile
            Setting up watches.
            Watches established.
            testfile MODIFY
            testfile MODIFY


            also you can redirect output to while statement :



            while read j
            do
            echo "file changed"
            break
            done < <(inotifywait -q -e modify /tmp/testfile)






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jun 2 at 15:38

























            answered Jun 3 '16 at 2:23









            بارپابابا

            1,6661429




            1,6661429






















                up vote
                4
                down vote













                filename="$1"

                m1=$(md5sum "$filename")

                while true; do

                # md5sum is computationally expensive, so check only once every 10 seconds
                sleep 10

                m2=$(md5sum "$filename")

                if [ "$m1" != "$m2" ] ; then
                echo "ERROR: File has changed!" >&2
                exit 1
                fi
                done





                share|improve this answer
























                  up vote
                  4
                  down vote













                  filename="$1"

                  m1=$(md5sum "$filename")

                  while true; do

                  # md5sum is computationally expensive, so check only once every 10 seconds
                  sleep 10

                  m2=$(md5sum "$filename")

                  if [ "$m1" != "$m2" ] ; then
                  echo "ERROR: File has changed!" >&2
                  exit 1
                  fi
                  done





                  share|improve this answer






















                    up vote
                    4
                    down vote










                    up vote
                    4
                    down vote









                    filename="$1"

                    m1=$(md5sum "$filename")

                    while true; do

                    # md5sum is computationally expensive, so check only once every 10 seconds
                    sleep 10

                    m2=$(md5sum "$filename")

                    if [ "$m1" != "$m2" ] ; then
                    echo "ERROR: File has changed!" >&2
                    exit 1
                    fi
                    done





                    share|improve this answer












                    filename="$1"

                    m1=$(md5sum "$filename")

                    while true; do

                    # md5sum is computationally expensive, so check only once every 10 seconds
                    sleep 10

                    m2=$(md5sum "$filename")

                    if [ "$m1" != "$m2" ] ; then
                    echo "ERROR: File has changed!" >&2
                    exit 1
                    fi
                    done






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jun 3 '16 at 2:06









                    cas

                    38k44495




                    38k44495




















                        up vote
                        0
                        down vote













                        Consider using md5sum, is safer to check real file modifications. This is script will return "The files are different" if a file diverge from the other, but when you equalize then, it will say the files are equal again.



                        #!/bin/bash

                        loop1() cut -d' ' -f1)
                        if [ "$md5f2" != "$md5f1" ]; then
                        echo "The files are different now."
                        #stop loop:
                        break
                        fi
                        done


                        loop2() cut -d' ' -f1)
                        if [ "$md5f2" == "$md5f1" ]; then
                        echo "The files are equal again."
                        #stop loop:
                        break
                        fi
                        done


                        while true; do
                        loop1 "$1" "$2"
                        loop2 "$1" "$2"
                        done


                        Save it as autocompare and run like:



                        ./autocompare file1 file2 





                        share|improve this answer






















                        • Not i corrected it 4 mins ago.
                          – Luciano Andress Martini
                          Jun 2 '16 at 15:07










                        • md5sum is the right approach, but that script is far more complicated than it needs to be. and it's comparing two different files, not just one file against possibly different versions of itself.
                          – cas
                          Jun 3 '16 at 2:05






                        • 3




                          md5sum might not be a perfect approach if this is a particularly huge file and you plan to monitor this continually (excessive i/o and cpu usage?)
                          – steve
                          Jun 4 '16 at 16:43














                        up vote
                        0
                        down vote













                        Consider using md5sum, is safer to check real file modifications. This is script will return "The files are different" if a file diverge from the other, but when you equalize then, it will say the files are equal again.



                        #!/bin/bash

                        loop1() cut -d' ' -f1)
                        if [ "$md5f2" != "$md5f1" ]; then
                        echo "The files are different now."
                        #stop loop:
                        break
                        fi
                        done


                        loop2() cut -d' ' -f1)
                        if [ "$md5f2" == "$md5f1" ]; then
                        echo "The files are equal again."
                        #stop loop:
                        break
                        fi
                        done


                        while true; do
                        loop1 "$1" "$2"
                        loop2 "$1" "$2"
                        done


                        Save it as autocompare and run like:



                        ./autocompare file1 file2 





                        share|improve this answer






















                        • Not i corrected it 4 mins ago.
                          – Luciano Andress Martini
                          Jun 2 '16 at 15:07










                        • md5sum is the right approach, but that script is far more complicated than it needs to be. and it's comparing two different files, not just one file against possibly different versions of itself.
                          – cas
                          Jun 3 '16 at 2:05






                        • 3




                          md5sum might not be a perfect approach if this is a particularly huge file and you plan to monitor this continually (excessive i/o and cpu usage?)
                          – steve
                          Jun 4 '16 at 16:43












                        up vote
                        0
                        down vote










                        up vote
                        0
                        down vote









                        Consider using md5sum, is safer to check real file modifications. This is script will return "The files are different" if a file diverge from the other, but when you equalize then, it will say the files are equal again.



                        #!/bin/bash

                        loop1() cut -d' ' -f1)
                        if [ "$md5f2" != "$md5f1" ]; then
                        echo "The files are different now."
                        #stop loop:
                        break
                        fi
                        done


                        loop2() cut -d' ' -f1)
                        if [ "$md5f2" == "$md5f1" ]; then
                        echo "The files are equal again."
                        #stop loop:
                        break
                        fi
                        done


                        while true; do
                        loop1 "$1" "$2"
                        loop2 "$1" "$2"
                        done


                        Save it as autocompare and run like:



                        ./autocompare file1 file2 





                        share|improve this answer














                        Consider using md5sum, is safer to check real file modifications. This is script will return "The files are different" if a file diverge from the other, but when you equalize then, it will say the files are equal again.



                        #!/bin/bash

                        loop1() cut -d' ' -f1)
                        if [ "$md5f2" != "$md5f1" ]; then
                        echo "The files are different now."
                        #stop loop:
                        break
                        fi
                        done


                        loop2() cut -d' ' -f1)
                        if [ "$md5f2" == "$md5f1" ]; then
                        echo "The files are equal again."
                        #stop loop:
                        break
                        fi
                        done


                        while true; do
                        loop1 "$1" "$2"
                        loop2 "$1" "$2"
                        done


                        Save it as autocompare and run like:



                        ./autocompare file1 file2 






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Jun 2 '16 at 15:02

























                        answered Jun 2 '16 at 14:56









                        Luciano Andress Martini

                        3,250830




                        3,250830











                        • Not i corrected it 4 mins ago.
                          – Luciano Andress Martini
                          Jun 2 '16 at 15:07










                        • md5sum is the right approach, but that script is far more complicated than it needs to be. and it's comparing two different files, not just one file against possibly different versions of itself.
                          – cas
                          Jun 3 '16 at 2:05






                        • 3




                          md5sum might not be a perfect approach if this is a particularly huge file and you plan to monitor this continually (excessive i/o and cpu usage?)
                          – steve
                          Jun 4 '16 at 16:43
















                        • Not i corrected it 4 mins ago.
                          – Luciano Andress Martini
                          Jun 2 '16 at 15:07










                        • md5sum is the right approach, but that script is far more complicated than it needs to be. and it's comparing two different files, not just one file against possibly different versions of itself.
                          – cas
                          Jun 3 '16 at 2:05






                        • 3




                          md5sum might not be a perfect approach if this is a particularly huge file and you plan to monitor this continually (excessive i/o and cpu usage?)
                          – steve
                          Jun 4 '16 at 16:43















                        Not i corrected it 4 mins ago.
                        – Luciano Andress Martini
                        Jun 2 '16 at 15:07




                        Not i corrected it 4 mins ago.
                        – Luciano Andress Martini
                        Jun 2 '16 at 15:07












                        md5sum is the right approach, but that script is far more complicated than it needs to be. and it's comparing two different files, not just one file against possibly different versions of itself.
                        – cas
                        Jun 3 '16 at 2:05




                        md5sum is the right approach, but that script is far more complicated than it needs to be. and it's comparing two different files, not just one file against possibly different versions of itself.
                        – cas
                        Jun 3 '16 at 2:05




                        3




                        3




                        md5sum might not be a perfect approach if this is a particularly huge file and you plan to monitor this continually (excessive i/o and cpu usage?)
                        – steve
                        Jun 4 '16 at 16:43




                        md5sum might not be a perfect approach if this is a particularly huge file and you plan to monitor this continually (excessive i/o and cpu usage?)
                        – steve
                        Jun 4 '16 at 16:43










                        up vote
                        0
                        down vote













                        If you do want to 'manually' check for change in the modification timestamp, as opposed to actual difference in the contents, you need:



                        • stat -c %y $1 consistently with the separating spaces and inside $( ... ). Even better, stat -c %y "$1" will work if your filename contains whitespace or any 'globbing' character


                        • test with classic [ ... ] or test ... and "$var" (because stat %y contains spaces; stat %Y would avoid that) or bash-enhanced [[ ... ]] which doesn't need quotes -- but not ( ... ) which does something completely different namely execute in a subshell


                        • some delay between loops so this doesn't completely hog your system


                         #!/bin/bash 
                        date=$(stat -c %y "$1")
                        while sleep 1; do date2=$(stat -c %y "$1")
                        if [[ $date2 != $date ]]; then echo "changed!"; break; fi
                        # possibly exit [status] instead of break
                        # or if you want to watch for another change, date=$date2
                        done





                        share|improve this answer
























                          up vote
                          0
                          down vote













                          If you do want to 'manually' check for change in the modification timestamp, as opposed to actual difference in the contents, you need:



                          • stat -c %y $1 consistently with the separating spaces and inside $( ... ). Even better, stat -c %y "$1" will work if your filename contains whitespace or any 'globbing' character


                          • test with classic [ ... ] or test ... and "$var" (because stat %y contains spaces; stat %Y would avoid that) or bash-enhanced [[ ... ]] which doesn't need quotes -- but not ( ... ) which does something completely different namely execute in a subshell


                          • some delay between loops so this doesn't completely hog your system


                           #!/bin/bash 
                          date=$(stat -c %y "$1")
                          while sleep 1; do date2=$(stat -c %y "$1")
                          if [[ $date2 != $date ]]; then echo "changed!"; break; fi
                          # possibly exit [status] instead of break
                          # or if you want to watch for another change, date=$date2
                          done





                          share|improve this answer






















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            If you do want to 'manually' check for change in the modification timestamp, as opposed to actual difference in the contents, you need:



                            • stat -c %y $1 consistently with the separating spaces and inside $( ... ). Even better, stat -c %y "$1" will work if your filename contains whitespace or any 'globbing' character


                            • test with classic [ ... ] or test ... and "$var" (because stat %y contains spaces; stat %Y would avoid that) or bash-enhanced [[ ... ]] which doesn't need quotes -- but not ( ... ) which does something completely different namely execute in a subshell


                            • some delay between loops so this doesn't completely hog your system


                             #!/bin/bash 
                            date=$(stat -c %y "$1")
                            while sleep 1; do date2=$(stat -c %y "$1")
                            if [[ $date2 != $date ]]; then echo "changed!"; break; fi
                            # possibly exit [status] instead of break
                            # or if you want to watch for another change, date=$date2
                            done





                            share|improve this answer












                            If you do want to 'manually' check for change in the modification timestamp, as opposed to actual difference in the contents, you need:



                            • stat -c %y $1 consistently with the separating spaces and inside $( ... ). Even better, stat -c %y "$1" will work if your filename contains whitespace or any 'globbing' character


                            • test with classic [ ... ] or test ... and "$var" (because stat %y contains spaces; stat %Y would avoid that) or bash-enhanced [[ ... ]] which doesn't need quotes -- but not ( ... ) which does something completely different namely execute in a subshell


                            • some delay between loops so this doesn't completely hog your system


                             #!/bin/bash 
                            date=$(stat -c %y "$1")
                            while sleep 1; do date2=$(stat -c %y "$1")
                            if [[ $date2 != $date ]]; then echo "changed!"; break; fi
                            # possibly exit [status] instead of break
                            # or if you want to watch for another change, date=$date2
                            done






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jun 4 '16 at 8:54









                            dave_thompson_085

                            2,0071810




                            2,0071810



























                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f287230%2fbash-script-to-check-if-a-file-is-modified-or-not%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Popular posts from this blog

                                How to check contact read email or not when send email to Individual?

                                Displaying single band from multi-band raster using QGIS

                                How many registers does an x86_64 CPU actually have?