Constantly check if file is modified bashAppend a “finished” command after a specific command is...

Shouldn't the "credit score" prevent Americans from going deeper and deeper into personal debt?

Can pay be witheld for hours cleaning up after closing time?

Why don't we use Cavea-B

What's /System/Volumes/Data?

Can you grapple/shove with the Hunter Ranger's Whirlwind Attack?

Defense against attacks using dictionaries

How to avoid using System.String with Rfc2898DeriveBytes in C#

Does adding the 'precise' tag to daggers break anything?

The sound of thunder's like a whip

Is "es" necessary in this sentence?

How much code would a codegolf golf if a codegolf could golf code?

Does Swashbuckler's Fancy Footwork apply if the attack was made with Booming Blade?

Are illustrations in novels frowned upon?

Why doesn't the Falcon-9 first stage use three legs to land?

Thread-safe, Convenient and Performant Random Number Generator

In an emergency, how do I find and share my position?

Is a butterfly one or two animals?

How can I support the recycling, but not the new production of aluminum?

Is refusing to concede in the face of an unstoppable Nexus combo punishable?

How to "know" if I have a passion?

What is the difference between a premise and an assumption in logic?

Can my boyfriend, who lives in the UK and has a Polish passport, visit me in the USA?

Was Tuvok bluffing when he said that Voyager's transporters rendered the Kazon weapons useless?

Why are delta bots so finicky?



Constantly check if file is modified bash


Append a “finished” command after a specific command is usedValidate File content in Bash ScriptBash script for beeping the number of hours not workingHow may I watch for the creation of a particular “trigger file”?Move file only if another file with different suffix existsunable to read functions from the bash scriptFind modified files between 2 timestamps using bash scriptHow can I check if a file can be created or truncated/overwritten in bash?bash copy file to link's referenceCheck if multiple files from a list (different paths) exist






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







8















I have a file called file1 I want in a script, whenever there is a change in it, do something, a beep sound actually. How do I do that?










share|improve this question

































    8















    I have a file called file1 I want in a script, whenever there is a change in it, do something, a beep sound actually. How do I do that?










    share|improve this question





























      8












      8








      8


      4






      I have a file called file1 I want in a script, whenever there is a change in it, do something, a beep sound actually. How do I do that?










      share|improve this question
















      I have a file called file1 I want in a script, whenever there is a change in it, do something, a beep sound actually. How do I do that?







      linux bash shell-script files monitoring






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 18 '17 at 7:33









      serenesat

      9391 gold badge9 silver badges22 bronze badges




      9391 gold badge9 silver badges22 bronze badges










      asked Nov 6 '14 at 9:35









      aDoNaDoN

      3262 gold badges5 silver badges20 bronze badges




      3262 gold badges5 silver badges20 bronze badges

























          7 Answers
          7






          active

          oldest

          votes


















          14














          If you have inotify-tools installed (at least that's the package name on Debian) when you can do something like this:



          while inotifywait -q -e modify filename >/dev/null; do
          echo "filename is changed"
          # do whatever else you need to do
          done


          This waits for the "modify" event to happen to the file named "filename". When that happens the inotifywait command outputs filename MODIFY (which we discard by sending the output to /dev/null) and then terminates, which causes the body of the loop to be entered.



          Read the manpage for inotifywait for more possibilities.






          share|improve this answer


























          • Good point, I hadn't read the manpage that well :-)

            – wurtel
            Nov 6 '14 at 15:01











          • You don't strictly need while. Also note that what a human considers a "modify" might not always work: this will catch an append for example, but it will not catch an editor such as vim (file watched is renamed or swapped with a backup), nor perl -i (in-place edit) which replaces the file with a new one. Once either of those happens, inotifywait will never return. Watching an inode and watching a filename aren't quite the same thing, so it depends on the use case.

            – mr.spuratic
            Nov 7 '14 at 12:48






          • 1





            You can add other events to wait for, e.g. move_self will catch renames. See the manpage for full listing of events.

            – wurtel
            Nov 7 '14 at 12:50



















          2














          Without inotifywait you can use this little script and a cron job (every minute or so):



          #!/usr/bin/env bash
          #
          # Provides : Check if a file is changed
          #
          # Limitations : none
          # Options : none
          # Requirements : bash, md5sum, cut
          #
          # Modified : 11|07|2014
          # Author : ItsMe
          # Reply to : n/a in public
          #
          # Editor : joe
          #
          #####################################
          #
          # OK - lets work
          #

          # what file do we want to monitor?
          # I did not include commandline options
          # but its easy to catch a command line option
          # and replace the defaul given here
          file=/foo/bar/nattebums/bla.txt

          # path to file's saved md5sum
          # I did not spend much effort in naming this file
          # if you ahve to test multiple files
          # so just use a commandline option and use the given
          # file name like: filename=$(basename "$file")
          fingerprintfile=/tmp/.bla.md5savefile

          # does the file exist?
          if [ ! -f $file ]
          then
          echo "ERROR: $file does not exist - aborting"
          exit 1
          fi

          # create the md5sum from the file to check
          filemd5=`md5sum $file | cut -d " " -f1`

          # check the md5 and
          # show an error when we check an empty file
          if [ -z $filemd5 ]
          then
          echo "The file is empty - aborting"
          exit 1
          else
          # pass silent
          :
          fi

          # do we have allready an saved fingerprint of this file?
          if [ -f $fingerprintfile ]
          then
          # yup - get the saved md5
          savedmd5=`cat $fingerprintfile`

          # check again if its empty
          if [ -z $savedmd5 ]
          then
          echo "The file is empty - aborting"
          exit 1
          fi

          #compare the saved md5 with the one we have now
          if [ "$savedmd5" = "$filemd5" ]
          then
          # pass silent
          :
          else
          echo "File has been changed"

          # this does an beep on your pc speaker (probably)
          # you get this character when you do:
          # CTRL+V CTRL+G
          # this is a bit creepy so you can use the 'beep' command
          # of your distro
          # or run some command you want to
          echo
          fi

          fi

          # save the current md5
          # sure you don't have to do this when the file hasn't changed
          # but you know I'm lazy and it works...
          echo $filemd5 > $fingerprintfile





          share|improve this answer

































            2














            Came looking for a one-liner on MacOS. Settled on the following. Compiled and added this tool to my path. This took less then 30 seconds.



            $ git clone git@github.com:sschober/kqwait.git
            $ cd kqwait
            $ make
            $ mv kqwait ~/bin
            $ chmod +x ~/bin/kqwait


            Next, I went to the directory in which I wished to do the watching. In this case, I wished to watch a markdown file for changes, and if changed issue a make.



            $ while true; do kqwait doc/my_file.md; make; done


            That's it.






            share|improve this answer





















            • 1





              as a note, worked well (if file contents change), appears to be OS X only, and you can install view brew install kqwait and you can pass multiple files to it like kqwait **/*

              – rogerdpack
              Oct 11 '16 at 17:53



















            0














            You probably don't need to compare md5sum if you have the diff utility available.



            if ! diff "$file1" "$file2" >/dev/null 2>&1; then
            echo "$file1 and $file2 does not match" >&2
            ## INSERT-YOUR-COMMAND/SCRIPT-HERE
            ## e.g. cp "$file1" "$file2"
            fi


            the ! negates e.g. true if the statement is false



            Caveat is you need the original file to compare with diff which (imo) the same as what md5sum script is doing above.






            share|improve this answer


























            • Use diff -q, if diff supports it.

              – muru
              Jan 13 '15 at 4:37











            • Ok let's try that: echo foo > foo.txt; echo bar > bar.txt; diff foo.txt bar.txt --> Files foo and bar differ ## (so much for being quite :))

              – Jetchisel
              Jan 13 '15 at 5:27













            • It is quieter than having every difference spelled out. -q means "report if only files differ", not how they differ. So diff -q stops comparing the moment a difference is seen, which can be very useful performance wise. See the GNU documentation, for example. If the whole point of your answer is being efficient by not using md5sum, then not using diff -q if available is defeating that point.

              – muru
              Jan 13 '15 at 5:32













            • Ok, -q "performance wise" that is good but it still prints something to stdout/stderr if the file differs from each other. I stated the ! which negate didn't I? what i'm after is the exit status of diff not being 0, ( See the GNU documentation) then take action.

              – Jetchisel
              Jan 13 '15 at 5:40













            • You're missing the point. I have no further desire to explain.

              – muru
              Jan 13 '15 at 5:41



















            0














            You can try entr command-line tool, e.g.



            $ ls file1 | entr beep





            share|improve this answer


























            • entr needs to be installed or at least that's the case for Ubuntu. It should be present in most distor repositories out there though.

              – rbaleksandar
              Nov 16 '17 at 9:32



















            0














            if you are checking changes in a git repo, you can use:



            #!/usr/bin/env bash

            diff="$(git diff | egrep some_file_name_or_file_path | cat)"

            if [[ -n "$diff" ]] ; then
            echo "==== Found changes: ===="
            echo "diff: $diff"
            exit 1
            else
            echo 'Code is not changed'
            fi






            share|improve this answer








            New contributor



            Alon Gouldman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.





























              0














              Done in 2 steps Tested and worked fine in both scenarios


              a. cp orginalfile fileneedto_be_changed'(Need to do only one Time)



              orginalfile=====>which supposed to be changed


              b.



              differencecount=`awk 'NR==FNR{a[$0];next}!($0 in a){print $0}' orginalfile fileneedto_be_changed|wc -l`

              if [ $differencecount -eq 0 ]
              then
              echo "NO changes in file"
              else
              echo "Noted there is changes in file"
              fi





              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',
                autoActivateHeartbeat: false,
                convertImagesToLinks: false,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                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%2f166341%2fconstantly-check-if-file-is-modified-bash%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                7 Answers
                7






                active

                oldest

                votes








                7 Answers
                7






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                14














                If you have inotify-tools installed (at least that's the package name on Debian) when you can do something like this:



                while inotifywait -q -e modify filename >/dev/null; do
                echo "filename is changed"
                # do whatever else you need to do
                done


                This waits for the "modify" event to happen to the file named "filename". When that happens the inotifywait command outputs filename MODIFY (which we discard by sending the output to /dev/null) and then terminates, which causes the body of the loop to be entered.



                Read the manpage for inotifywait for more possibilities.






                share|improve this answer


























                • Good point, I hadn't read the manpage that well :-)

                  – wurtel
                  Nov 6 '14 at 15:01











                • You don't strictly need while. Also note that what a human considers a "modify" might not always work: this will catch an append for example, but it will not catch an editor such as vim (file watched is renamed or swapped with a backup), nor perl -i (in-place edit) which replaces the file with a new one. Once either of those happens, inotifywait will never return. Watching an inode and watching a filename aren't quite the same thing, so it depends on the use case.

                  – mr.spuratic
                  Nov 7 '14 at 12:48






                • 1





                  You can add other events to wait for, e.g. move_self will catch renames. See the manpage for full listing of events.

                  – wurtel
                  Nov 7 '14 at 12:50
















                14














                If you have inotify-tools installed (at least that's the package name on Debian) when you can do something like this:



                while inotifywait -q -e modify filename >/dev/null; do
                echo "filename is changed"
                # do whatever else you need to do
                done


                This waits for the "modify" event to happen to the file named "filename". When that happens the inotifywait command outputs filename MODIFY (which we discard by sending the output to /dev/null) and then terminates, which causes the body of the loop to be entered.



                Read the manpage for inotifywait for more possibilities.






                share|improve this answer


























                • Good point, I hadn't read the manpage that well :-)

                  – wurtel
                  Nov 6 '14 at 15:01











                • You don't strictly need while. Also note that what a human considers a "modify" might not always work: this will catch an append for example, but it will not catch an editor such as vim (file watched is renamed or swapped with a backup), nor perl -i (in-place edit) which replaces the file with a new one. Once either of those happens, inotifywait will never return. Watching an inode and watching a filename aren't quite the same thing, so it depends on the use case.

                  – mr.spuratic
                  Nov 7 '14 at 12:48






                • 1





                  You can add other events to wait for, e.g. move_self will catch renames. See the manpage for full listing of events.

                  – wurtel
                  Nov 7 '14 at 12:50














                14












                14








                14







                If you have inotify-tools installed (at least that's the package name on Debian) when you can do something like this:



                while inotifywait -q -e modify filename >/dev/null; do
                echo "filename is changed"
                # do whatever else you need to do
                done


                This waits for the "modify" event to happen to the file named "filename". When that happens the inotifywait command outputs filename MODIFY (which we discard by sending the output to /dev/null) and then terminates, which causes the body of the loop to be entered.



                Read the manpage for inotifywait for more possibilities.






                share|improve this answer













                If you have inotify-tools installed (at least that's the package name on Debian) when you can do something like this:



                while inotifywait -q -e modify filename >/dev/null; do
                echo "filename is changed"
                # do whatever else you need to do
                done


                This waits for the "modify" event to happen to the file named "filename". When that happens the inotifywait command outputs filename MODIFY (which we discard by sending the output to /dev/null) and then terminates, which causes the body of the loop to be entered.



                Read the manpage for inotifywait for more possibilities.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 6 '14 at 9:51









                wurtelwurtel

                11.8k1 gold badge16 silver badges29 bronze badges




                11.8k1 gold badge16 silver badges29 bronze badges
















                • Good point, I hadn't read the manpage that well :-)

                  – wurtel
                  Nov 6 '14 at 15:01











                • You don't strictly need while. Also note that what a human considers a "modify" might not always work: this will catch an append for example, but it will not catch an editor such as vim (file watched is renamed or swapped with a backup), nor perl -i (in-place edit) which replaces the file with a new one. Once either of those happens, inotifywait will never return. Watching an inode and watching a filename aren't quite the same thing, so it depends on the use case.

                  – mr.spuratic
                  Nov 7 '14 at 12:48






                • 1





                  You can add other events to wait for, e.g. move_self will catch renames. See the manpage for full listing of events.

                  – wurtel
                  Nov 7 '14 at 12:50



















                • Good point, I hadn't read the manpage that well :-)

                  – wurtel
                  Nov 6 '14 at 15:01











                • You don't strictly need while. Also note that what a human considers a "modify" might not always work: this will catch an append for example, but it will not catch an editor such as vim (file watched is renamed or swapped with a backup), nor perl -i (in-place edit) which replaces the file with a new one. Once either of those happens, inotifywait will never return. Watching an inode and watching a filename aren't quite the same thing, so it depends on the use case.

                  – mr.spuratic
                  Nov 7 '14 at 12:48






                • 1





                  You can add other events to wait for, e.g. move_self will catch renames. See the manpage for full listing of events.

                  – wurtel
                  Nov 7 '14 at 12:50

















                Good point, I hadn't read the manpage that well :-)

                – wurtel
                Nov 6 '14 at 15:01





                Good point, I hadn't read the manpage that well :-)

                – wurtel
                Nov 6 '14 at 15:01













                You don't strictly need while. Also note that what a human considers a "modify" might not always work: this will catch an append for example, but it will not catch an editor such as vim (file watched is renamed or swapped with a backup), nor perl -i (in-place edit) which replaces the file with a new one. Once either of those happens, inotifywait will never return. Watching an inode and watching a filename aren't quite the same thing, so it depends on the use case.

                – mr.spuratic
                Nov 7 '14 at 12:48





                You don't strictly need while. Also note that what a human considers a "modify" might not always work: this will catch an append for example, but it will not catch an editor such as vim (file watched is renamed or swapped with a backup), nor perl -i (in-place edit) which replaces the file with a new one. Once either of those happens, inotifywait will never return. Watching an inode and watching a filename aren't quite the same thing, so it depends on the use case.

                – mr.spuratic
                Nov 7 '14 at 12:48




                1




                1





                You can add other events to wait for, e.g. move_self will catch renames. See the manpage for full listing of events.

                – wurtel
                Nov 7 '14 at 12:50





                You can add other events to wait for, e.g. move_self will catch renames. See the manpage for full listing of events.

                – wurtel
                Nov 7 '14 at 12:50













                2














                Without inotifywait you can use this little script and a cron job (every minute or so):



                #!/usr/bin/env bash
                #
                # Provides : Check if a file is changed
                #
                # Limitations : none
                # Options : none
                # Requirements : bash, md5sum, cut
                #
                # Modified : 11|07|2014
                # Author : ItsMe
                # Reply to : n/a in public
                #
                # Editor : joe
                #
                #####################################
                #
                # OK - lets work
                #

                # what file do we want to monitor?
                # I did not include commandline options
                # but its easy to catch a command line option
                # and replace the defaul given here
                file=/foo/bar/nattebums/bla.txt

                # path to file's saved md5sum
                # I did not spend much effort in naming this file
                # if you ahve to test multiple files
                # so just use a commandline option and use the given
                # file name like: filename=$(basename "$file")
                fingerprintfile=/tmp/.bla.md5savefile

                # does the file exist?
                if [ ! -f $file ]
                then
                echo "ERROR: $file does not exist - aborting"
                exit 1
                fi

                # create the md5sum from the file to check
                filemd5=`md5sum $file | cut -d " " -f1`

                # check the md5 and
                # show an error when we check an empty file
                if [ -z $filemd5 ]
                then
                echo "The file is empty - aborting"
                exit 1
                else
                # pass silent
                :
                fi

                # do we have allready an saved fingerprint of this file?
                if [ -f $fingerprintfile ]
                then
                # yup - get the saved md5
                savedmd5=`cat $fingerprintfile`

                # check again if its empty
                if [ -z $savedmd5 ]
                then
                echo "The file is empty - aborting"
                exit 1
                fi

                #compare the saved md5 with the one we have now
                if [ "$savedmd5" = "$filemd5" ]
                then
                # pass silent
                :
                else
                echo "File has been changed"

                # this does an beep on your pc speaker (probably)
                # you get this character when you do:
                # CTRL+V CTRL+G
                # this is a bit creepy so you can use the 'beep' command
                # of your distro
                # or run some command you want to
                echo
                fi

                fi

                # save the current md5
                # sure you don't have to do this when the file hasn't changed
                # but you know I'm lazy and it works...
                echo $filemd5 > $fingerprintfile





                share|improve this answer






























                  2














                  Without inotifywait you can use this little script and a cron job (every minute or so):



                  #!/usr/bin/env bash
                  #
                  # Provides : Check if a file is changed
                  #
                  # Limitations : none
                  # Options : none
                  # Requirements : bash, md5sum, cut
                  #
                  # Modified : 11|07|2014
                  # Author : ItsMe
                  # Reply to : n/a in public
                  #
                  # Editor : joe
                  #
                  #####################################
                  #
                  # OK - lets work
                  #

                  # what file do we want to monitor?
                  # I did not include commandline options
                  # but its easy to catch a command line option
                  # and replace the defaul given here
                  file=/foo/bar/nattebums/bla.txt

                  # path to file's saved md5sum
                  # I did not spend much effort in naming this file
                  # if you ahve to test multiple files
                  # so just use a commandline option and use the given
                  # file name like: filename=$(basename "$file")
                  fingerprintfile=/tmp/.bla.md5savefile

                  # does the file exist?
                  if [ ! -f $file ]
                  then
                  echo "ERROR: $file does not exist - aborting"
                  exit 1
                  fi

                  # create the md5sum from the file to check
                  filemd5=`md5sum $file | cut -d " " -f1`

                  # check the md5 and
                  # show an error when we check an empty file
                  if [ -z $filemd5 ]
                  then
                  echo "The file is empty - aborting"
                  exit 1
                  else
                  # pass silent
                  :
                  fi

                  # do we have allready an saved fingerprint of this file?
                  if [ -f $fingerprintfile ]
                  then
                  # yup - get the saved md5
                  savedmd5=`cat $fingerprintfile`

                  # check again if its empty
                  if [ -z $savedmd5 ]
                  then
                  echo "The file is empty - aborting"
                  exit 1
                  fi

                  #compare the saved md5 with the one we have now
                  if [ "$savedmd5" = "$filemd5" ]
                  then
                  # pass silent
                  :
                  else
                  echo "File has been changed"

                  # this does an beep on your pc speaker (probably)
                  # you get this character when you do:
                  # CTRL+V CTRL+G
                  # this is a bit creepy so you can use the 'beep' command
                  # of your distro
                  # or run some command you want to
                  echo
                  fi

                  fi

                  # save the current md5
                  # sure you don't have to do this when the file hasn't changed
                  # but you know I'm lazy and it works...
                  echo $filemd5 > $fingerprintfile





                  share|improve this answer




























                    2












                    2








                    2







                    Without inotifywait you can use this little script and a cron job (every minute or so):



                    #!/usr/bin/env bash
                    #
                    # Provides : Check if a file is changed
                    #
                    # Limitations : none
                    # Options : none
                    # Requirements : bash, md5sum, cut
                    #
                    # Modified : 11|07|2014
                    # Author : ItsMe
                    # Reply to : n/a in public
                    #
                    # Editor : joe
                    #
                    #####################################
                    #
                    # OK - lets work
                    #

                    # what file do we want to monitor?
                    # I did not include commandline options
                    # but its easy to catch a command line option
                    # and replace the defaul given here
                    file=/foo/bar/nattebums/bla.txt

                    # path to file's saved md5sum
                    # I did not spend much effort in naming this file
                    # if you ahve to test multiple files
                    # so just use a commandline option and use the given
                    # file name like: filename=$(basename "$file")
                    fingerprintfile=/tmp/.bla.md5savefile

                    # does the file exist?
                    if [ ! -f $file ]
                    then
                    echo "ERROR: $file does not exist - aborting"
                    exit 1
                    fi

                    # create the md5sum from the file to check
                    filemd5=`md5sum $file | cut -d " " -f1`

                    # check the md5 and
                    # show an error when we check an empty file
                    if [ -z $filemd5 ]
                    then
                    echo "The file is empty - aborting"
                    exit 1
                    else
                    # pass silent
                    :
                    fi

                    # do we have allready an saved fingerprint of this file?
                    if [ -f $fingerprintfile ]
                    then
                    # yup - get the saved md5
                    savedmd5=`cat $fingerprintfile`

                    # check again if its empty
                    if [ -z $savedmd5 ]
                    then
                    echo "The file is empty - aborting"
                    exit 1
                    fi

                    #compare the saved md5 with the one we have now
                    if [ "$savedmd5" = "$filemd5" ]
                    then
                    # pass silent
                    :
                    else
                    echo "File has been changed"

                    # this does an beep on your pc speaker (probably)
                    # you get this character when you do:
                    # CTRL+V CTRL+G
                    # this is a bit creepy so you can use the 'beep' command
                    # of your distro
                    # or run some command you want to
                    echo
                    fi

                    fi

                    # save the current md5
                    # sure you don't have to do this when the file hasn't changed
                    # but you know I'm lazy and it works...
                    echo $filemd5 > $fingerprintfile





                    share|improve this answer













                    Without inotifywait you can use this little script and a cron job (every minute or so):



                    #!/usr/bin/env bash
                    #
                    # Provides : Check if a file is changed
                    #
                    # Limitations : none
                    # Options : none
                    # Requirements : bash, md5sum, cut
                    #
                    # Modified : 11|07|2014
                    # Author : ItsMe
                    # Reply to : n/a in public
                    #
                    # Editor : joe
                    #
                    #####################################
                    #
                    # OK - lets work
                    #

                    # what file do we want to monitor?
                    # I did not include commandline options
                    # but its easy to catch a command line option
                    # and replace the defaul given here
                    file=/foo/bar/nattebums/bla.txt

                    # path to file's saved md5sum
                    # I did not spend much effort in naming this file
                    # if you ahve to test multiple files
                    # so just use a commandline option and use the given
                    # file name like: filename=$(basename "$file")
                    fingerprintfile=/tmp/.bla.md5savefile

                    # does the file exist?
                    if [ ! -f $file ]
                    then
                    echo "ERROR: $file does not exist - aborting"
                    exit 1
                    fi

                    # create the md5sum from the file to check
                    filemd5=`md5sum $file | cut -d " " -f1`

                    # check the md5 and
                    # show an error when we check an empty file
                    if [ -z $filemd5 ]
                    then
                    echo "The file is empty - aborting"
                    exit 1
                    else
                    # pass silent
                    :
                    fi

                    # do we have allready an saved fingerprint of this file?
                    if [ -f $fingerprintfile ]
                    then
                    # yup - get the saved md5
                    savedmd5=`cat $fingerprintfile`

                    # check again if its empty
                    if [ -z $savedmd5 ]
                    then
                    echo "The file is empty - aborting"
                    exit 1
                    fi

                    #compare the saved md5 with the one we have now
                    if [ "$savedmd5" = "$filemd5" ]
                    then
                    # pass silent
                    :
                    else
                    echo "File has been changed"

                    # this does an beep on your pc speaker (probably)
                    # you get this character when you do:
                    # CTRL+V CTRL+G
                    # this is a bit creepy so you can use the 'beep' command
                    # of your distro
                    # or run some command you want to
                    echo
                    fi

                    fi

                    # save the current md5
                    # sure you don't have to do this when the file hasn't changed
                    # but you know I'm lazy and it works...
                    echo $filemd5 > $fingerprintfile






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 7 '14 at 11:16









                    ItsMeItsMe

                    1945 bronze badges




                    1945 bronze badges


























                        2














                        Came looking for a one-liner on MacOS. Settled on the following. Compiled and added this tool to my path. This took less then 30 seconds.



                        $ git clone git@github.com:sschober/kqwait.git
                        $ cd kqwait
                        $ make
                        $ mv kqwait ~/bin
                        $ chmod +x ~/bin/kqwait


                        Next, I went to the directory in which I wished to do the watching. In this case, I wished to watch a markdown file for changes, and if changed issue a make.



                        $ while true; do kqwait doc/my_file.md; make; done


                        That's it.






                        share|improve this answer





















                        • 1





                          as a note, worked well (if file contents change), appears to be OS X only, and you can install view brew install kqwait and you can pass multiple files to it like kqwait **/*

                          – rogerdpack
                          Oct 11 '16 at 17:53
















                        2














                        Came looking for a one-liner on MacOS. Settled on the following. Compiled and added this tool to my path. This took less then 30 seconds.



                        $ git clone git@github.com:sschober/kqwait.git
                        $ cd kqwait
                        $ make
                        $ mv kqwait ~/bin
                        $ chmod +x ~/bin/kqwait


                        Next, I went to the directory in which I wished to do the watching. In this case, I wished to watch a markdown file for changes, and if changed issue a make.



                        $ while true; do kqwait doc/my_file.md; make; done


                        That's it.






                        share|improve this answer





















                        • 1





                          as a note, worked well (if file contents change), appears to be OS X only, and you can install view brew install kqwait and you can pass multiple files to it like kqwait **/*

                          – rogerdpack
                          Oct 11 '16 at 17:53














                        2












                        2








                        2







                        Came looking for a one-liner on MacOS. Settled on the following. Compiled and added this tool to my path. This took less then 30 seconds.



                        $ git clone git@github.com:sschober/kqwait.git
                        $ cd kqwait
                        $ make
                        $ mv kqwait ~/bin
                        $ chmod +x ~/bin/kqwait


                        Next, I went to the directory in which I wished to do the watching. In this case, I wished to watch a markdown file for changes, and if changed issue a make.



                        $ while true; do kqwait doc/my_file.md; make; done


                        That's it.






                        share|improve this answer













                        Came looking for a one-liner on MacOS. Settled on the following. Compiled and added this tool to my path. This took less then 30 seconds.



                        $ git clone git@github.com:sschober/kqwait.git
                        $ cd kqwait
                        $ make
                        $ mv kqwait ~/bin
                        $ chmod +x ~/bin/kqwait


                        Next, I went to the directory in which I wished to do the watching. In this case, I wished to watch a markdown file for changes, and if changed issue a make.



                        $ while true; do kqwait doc/my_file.md; make; done


                        That's it.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Apr 24 '16 at 18:49









                        Joshua CookJoshua Cook

                        1331 silver badge6 bronze badges




                        1331 silver badge6 bronze badges











                        • 1





                          as a note, worked well (if file contents change), appears to be OS X only, and you can install view brew install kqwait and you can pass multiple files to it like kqwait **/*

                          – rogerdpack
                          Oct 11 '16 at 17:53














                        • 1





                          as a note, worked well (if file contents change), appears to be OS X only, and you can install view brew install kqwait and you can pass multiple files to it like kqwait **/*

                          – rogerdpack
                          Oct 11 '16 at 17:53








                        1




                        1





                        as a note, worked well (if file contents change), appears to be OS X only, and you can install view brew install kqwait and you can pass multiple files to it like kqwait **/*

                        – rogerdpack
                        Oct 11 '16 at 17:53





                        as a note, worked well (if file contents change), appears to be OS X only, and you can install view brew install kqwait and you can pass multiple files to it like kqwait **/*

                        – rogerdpack
                        Oct 11 '16 at 17:53











                        0














                        You probably don't need to compare md5sum if you have the diff utility available.



                        if ! diff "$file1" "$file2" >/dev/null 2>&1; then
                        echo "$file1 and $file2 does not match" >&2
                        ## INSERT-YOUR-COMMAND/SCRIPT-HERE
                        ## e.g. cp "$file1" "$file2"
                        fi


                        the ! negates e.g. true if the statement is false



                        Caveat is you need the original file to compare with diff which (imo) the same as what md5sum script is doing above.






                        share|improve this answer


























                        • Use diff -q, if diff supports it.

                          – muru
                          Jan 13 '15 at 4:37











                        • Ok let's try that: echo foo > foo.txt; echo bar > bar.txt; diff foo.txt bar.txt --> Files foo and bar differ ## (so much for being quite :))

                          – Jetchisel
                          Jan 13 '15 at 5:27













                        • It is quieter than having every difference spelled out. -q means "report if only files differ", not how they differ. So diff -q stops comparing the moment a difference is seen, which can be very useful performance wise. See the GNU documentation, for example. If the whole point of your answer is being efficient by not using md5sum, then not using diff -q if available is defeating that point.

                          – muru
                          Jan 13 '15 at 5:32













                        • Ok, -q "performance wise" that is good but it still prints something to stdout/stderr if the file differs from each other. I stated the ! which negate didn't I? what i'm after is the exit status of diff not being 0, ( See the GNU documentation) then take action.

                          – Jetchisel
                          Jan 13 '15 at 5:40













                        • You're missing the point. I have no further desire to explain.

                          – muru
                          Jan 13 '15 at 5:41
















                        0














                        You probably don't need to compare md5sum if you have the diff utility available.



                        if ! diff "$file1" "$file2" >/dev/null 2>&1; then
                        echo "$file1 and $file2 does not match" >&2
                        ## INSERT-YOUR-COMMAND/SCRIPT-HERE
                        ## e.g. cp "$file1" "$file2"
                        fi


                        the ! negates e.g. true if the statement is false



                        Caveat is you need the original file to compare with diff which (imo) the same as what md5sum script is doing above.






                        share|improve this answer


























                        • Use diff -q, if diff supports it.

                          – muru
                          Jan 13 '15 at 4:37











                        • Ok let's try that: echo foo > foo.txt; echo bar > bar.txt; diff foo.txt bar.txt --> Files foo and bar differ ## (so much for being quite :))

                          – Jetchisel
                          Jan 13 '15 at 5:27













                        • It is quieter than having every difference spelled out. -q means "report if only files differ", not how they differ. So diff -q stops comparing the moment a difference is seen, which can be very useful performance wise. See the GNU documentation, for example. If the whole point of your answer is being efficient by not using md5sum, then not using diff -q if available is defeating that point.

                          – muru
                          Jan 13 '15 at 5:32













                        • Ok, -q "performance wise" that is good but it still prints something to stdout/stderr if the file differs from each other. I stated the ! which negate didn't I? what i'm after is the exit status of diff not being 0, ( See the GNU documentation) then take action.

                          – Jetchisel
                          Jan 13 '15 at 5:40













                        • You're missing the point. I have no further desire to explain.

                          – muru
                          Jan 13 '15 at 5:41














                        0












                        0








                        0







                        You probably don't need to compare md5sum if you have the diff utility available.



                        if ! diff "$file1" "$file2" >/dev/null 2>&1; then
                        echo "$file1 and $file2 does not match" >&2
                        ## INSERT-YOUR-COMMAND/SCRIPT-HERE
                        ## e.g. cp "$file1" "$file2"
                        fi


                        the ! negates e.g. true if the statement is false



                        Caveat is you need the original file to compare with diff which (imo) the same as what md5sum script is doing above.






                        share|improve this answer













                        You probably don't need to compare md5sum if you have the diff utility available.



                        if ! diff "$file1" "$file2" >/dev/null 2>&1; then
                        echo "$file1 and $file2 does not match" >&2
                        ## INSERT-YOUR-COMMAND/SCRIPT-HERE
                        ## e.g. cp "$file1" "$file2"
                        fi


                        the ! negates e.g. true if the statement is false



                        Caveat is you need the original file to compare with diff which (imo) the same as what md5sum script is doing above.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Jan 13 '15 at 4:27









                        JetchiselJetchisel

                        111 bronze badge




                        111 bronze badge
















                        • Use diff -q, if diff supports it.

                          – muru
                          Jan 13 '15 at 4:37











                        • Ok let's try that: echo foo > foo.txt; echo bar > bar.txt; diff foo.txt bar.txt --> Files foo and bar differ ## (so much for being quite :))

                          – Jetchisel
                          Jan 13 '15 at 5:27













                        • It is quieter than having every difference spelled out. -q means "report if only files differ", not how they differ. So diff -q stops comparing the moment a difference is seen, which can be very useful performance wise. See the GNU documentation, for example. If the whole point of your answer is being efficient by not using md5sum, then not using diff -q if available is defeating that point.

                          – muru
                          Jan 13 '15 at 5:32













                        • Ok, -q "performance wise" that is good but it still prints something to stdout/stderr if the file differs from each other. I stated the ! which negate didn't I? what i'm after is the exit status of diff not being 0, ( See the GNU documentation) then take action.

                          – Jetchisel
                          Jan 13 '15 at 5:40













                        • You're missing the point. I have no further desire to explain.

                          – muru
                          Jan 13 '15 at 5:41



















                        • Use diff -q, if diff supports it.

                          – muru
                          Jan 13 '15 at 4:37











                        • Ok let's try that: echo foo > foo.txt; echo bar > bar.txt; diff foo.txt bar.txt --> Files foo and bar differ ## (so much for being quite :))

                          – Jetchisel
                          Jan 13 '15 at 5:27













                        • It is quieter than having every difference spelled out. -q means "report if only files differ", not how they differ. So diff -q stops comparing the moment a difference is seen, which can be very useful performance wise. See the GNU documentation, for example. If the whole point of your answer is being efficient by not using md5sum, then not using diff -q if available is defeating that point.

                          – muru
                          Jan 13 '15 at 5:32













                        • Ok, -q "performance wise" that is good but it still prints something to stdout/stderr if the file differs from each other. I stated the ! which negate didn't I? what i'm after is the exit status of diff not being 0, ( See the GNU documentation) then take action.

                          – Jetchisel
                          Jan 13 '15 at 5:40













                        • You're missing the point. I have no further desire to explain.

                          – muru
                          Jan 13 '15 at 5:41

















                        Use diff -q, if diff supports it.

                        – muru
                        Jan 13 '15 at 4:37





                        Use diff -q, if diff supports it.

                        – muru
                        Jan 13 '15 at 4:37













                        Ok let's try that: echo foo > foo.txt; echo bar > bar.txt; diff foo.txt bar.txt --> Files foo and bar differ ## (so much for being quite :))

                        – Jetchisel
                        Jan 13 '15 at 5:27







                        Ok let's try that: echo foo > foo.txt; echo bar > bar.txt; diff foo.txt bar.txt --> Files foo and bar differ ## (so much for being quite :))

                        – Jetchisel
                        Jan 13 '15 at 5:27















                        It is quieter than having every difference spelled out. -q means "report if only files differ", not how they differ. So diff -q stops comparing the moment a difference is seen, which can be very useful performance wise. See the GNU documentation, for example. If the whole point of your answer is being efficient by not using md5sum, then not using diff -q if available is defeating that point.

                        – muru
                        Jan 13 '15 at 5:32







                        It is quieter than having every difference spelled out. -q means "report if only files differ", not how they differ. So diff -q stops comparing the moment a difference is seen, which can be very useful performance wise. See the GNU documentation, for example. If the whole point of your answer is being efficient by not using md5sum, then not using diff -q if available is defeating that point.

                        – muru
                        Jan 13 '15 at 5:32















                        Ok, -q "performance wise" that is good but it still prints something to stdout/stderr if the file differs from each other. I stated the ! which negate didn't I? what i'm after is the exit status of diff not being 0, ( See the GNU documentation) then take action.

                        – Jetchisel
                        Jan 13 '15 at 5:40







                        Ok, -q "performance wise" that is good but it still prints something to stdout/stderr if the file differs from each other. I stated the ! which negate didn't I? what i'm after is the exit status of diff not being 0, ( See the GNU documentation) then take action.

                        – Jetchisel
                        Jan 13 '15 at 5:40















                        You're missing the point. I have no further desire to explain.

                        – muru
                        Jan 13 '15 at 5:41





                        You're missing the point. I have no further desire to explain.

                        – muru
                        Jan 13 '15 at 5:41











                        0














                        You can try entr command-line tool, e.g.



                        $ ls file1 | entr beep





                        share|improve this answer


























                        • entr needs to be installed or at least that's the case for Ubuntu. It should be present in most distor repositories out there though.

                          – rbaleksandar
                          Nov 16 '17 at 9:32
















                        0














                        You can try entr command-line tool, e.g.



                        $ ls file1 | entr beep





                        share|improve this answer


























                        • entr needs to be installed or at least that's the case for Ubuntu. It should be present in most distor repositories out there though.

                          – rbaleksandar
                          Nov 16 '17 at 9:32














                        0












                        0








                        0







                        You can try entr command-line tool, e.g.



                        $ ls file1 | entr beep





                        share|improve this answer













                        You can try entr command-line tool, e.g.



                        $ ls file1 | entr beep






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Jul 6 '16 at 17:04









                        kenorbkenorb

                        10.1k4 gold badges82 silver badges124 bronze badges




                        10.1k4 gold badges82 silver badges124 bronze badges
















                        • entr needs to be installed or at least that's the case for Ubuntu. It should be present in most distor repositories out there though.

                          – rbaleksandar
                          Nov 16 '17 at 9:32



















                        • entr needs to be installed or at least that's the case for Ubuntu. It should be present in most distor repositories out there though.

                          – rbaleksandar
                          Nov 16 '17 at 9:32

















                        entr needs to be installed or at least that's the case for Ubuntu. It should be present in most distor repositories out there though.

                        – rbaleksandar
                        Nov 16 '17 at 9:32





                        entr needs to be installed or at least that's the case for Ubuntu. It should be present in most distor repositories out there though.

                        – rbaleksandar
                        Nov 16 '17 at 9:32











                        0














                        if you are checking changes in a git repo, you can use:



                        #!/usr/bin/env bash

                        diff="$(git diff | egrep some_file_name_or_file_path | cat)"

                        if [[ -n "$diff" ]] ; then
                        echo "==== Found changes: ===="
                        echo "diff: $diff"
                        exit 1
                        else
                        echo 'Code is not changed'
                        fi






                        share|improve this answer








                        New contributor



                        Alon Gouldman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.


























                          0














                          if you are checking changes in a git repo, you can use:



                          #!/usr/bin/env bash

                          diff="$(git diff | egrep some_file_name_or_file_path | cat)"

                          if [[ -n "$diff" ]] ; then
                          echo "==== Found changes: ===="
                          echo "diff: $diff"
                          exit 1
                          else
                          echo 'Code is not changed'
                          fi






                          share|improve this answer








                          New contributor



                          Alon Gouldman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.
























                            0












                            0








                            0







                            if you are checking changes in a git repo, you can use:



                            #!/usr/bin/env bash

                            diff="$(git diff | egrep some_file_name_or_file_path | cat)"

                            if [[ -n "$diff" ]] ; then
                            echo "==== Found changes: ===="
                            echo "diff: $diff"
                            exit 1
                            else
                            echo 'Code is not changed'
                            fi






                            share|improve this answer








                            New contributor



                            Alon Gouldman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.









                            if you are checking changes in a git repo, you can use:



                            #!/usr/bin/env bash

                            diff="$(git diff | egrep some_file_name_or_file_path | cat)"

                            if [[ -n "$diff" ]] ; then
                            echo "==== Found changes: ===="
                            echo "diff: $diff"
                            exit 1
                            else
                            echo 'Code is not changed'
                            fi







                            share|improve this answer








                            New contributor



                            Alon Gouldman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.








                            share|improve this answer



                            share|improve this answer






                            New contributor



                            Alon Gouldman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.








                            answered 2 days ago









                            Alon GouldmanAlon Gouldman

                            1




                            1




                            New contributor



                            Alon Gouldman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.




                            New contributor




                            Alon Gouldman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.




























                                0














                                Done in 2 steps Tested and worked fine in both scenarios


                                a. cp orginalfile fileneedto_be_changed'(Need to do only one Time)



                                orginalfile=====>which supposed to be changed


                                b.



                                differencecount=`awk 'NR==FNR{a[$0];next}!($0 in a){print $0}' orginalfile fileneedto_be_changed|wc -l`

                                if [ $differencecount -eq 0 ]
                                then
                                echo "NO changes in file"
                                else
                                echo "Noted there is changes in file"
                                fi





                                share|improve this answer






























                                  0














                                  Done in 2 steps Tested and worked fine in both scenarios


                                  a. cp orginalfile fileneedto_be_changed'(Need to do only one Time)



                                  orginalfile=====>which supposed to be changed


                                  b.



                                  differencecount=`awk 'NR==FNR{a[$0];next}!($0 in a){print $0}' orginalfile fileneedto_be_changed|wc -l`

                                  if [ $differencecount -eq 0 ]
                                  then
                                  echo "NO changes in file"
                                  else
                                  echo "Noted there is changes in file"
                                  fi





                                  share|improve this answer




























                                    0












                                    0








                                    0







                                    Done in 2 steps Tested and worked fine in both scenarios


                                    a. cp orginalfile fileneedto_be_changed'(Need to do only one Time)



                                    orginalfile=====>which supposed to be changed


                                    b.



                                    differencecount=`awk 'NR==FNR{a[$0];next}!($0 in a){print $0}' orginalfile fileneedto_be_changed|wc -l`

                                    if [ $differencecount -eq 0 ]
                                    then
                                    echo "NO changes in file"
                                    else
                                    echo "Noted there is changes in file"
                                    fi





                                    share|improve this answer













                                    Done in 2 steps Tested and worked fine in both scenarios


                                    a. cp orginalfile fileneedto_be_changed'(Need to do only one Time)



                                    orginalfile=====>which supposed to be changed


                                    b.



                                    differencecount=`awk 'NR==FNR{a[$0];next}!($0 in a){print $0}' orginalfile fileneedto_be_changed|wc -l`

                                    if [ $differencecount -eq 0 ]
                                    then
                                    echo "NO changes in file"
                                    else
                                    echo "Noted there is changes in file"
                                    fi






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered 2 days ago









                                    Praveen Kumar BSPraveen Kumar BS

                                    2,4072 gold badges3 silver badges11 bronze badges




                                    2,4072 gold badges3 silver badges11 bronze badges

































                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f166341%2fconstantly-check-if-file-is-modified-bash%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Hudson River Historic District Contents Geography History The district today Aesthetics Cultural...

                                        The number designs the writing. Feandra Aversely Definition: The act of ingrafting a sprig or shoot of one...

                                        Ayherre Geografie Demografie Externe links Navigatiemenu43° 23′ NB, 1° 15′ WL43° 23′ NB, 1°...