Bash script to check if a file is modified or not
Clash Royale CLAN TAG#URR8PPP
up vote
4
down vote
favorite
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
add a comment |Â
up vote
4
down vote
favorite
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
shellcheck will report some warnings, if that's what you're asking about....
â drewbenn
Jun 4 '16 at 6:42
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
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
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
bash shell-script
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
add a comment |Â
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
add a comment |Â
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)
add a comment |Â
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
add a comment |Â
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
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
add a comment |Â
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' charactertest with classic
[ ... ]
ortest ...
and"$var"
(becausestat %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 subshellsome 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
add a comment |Â
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)
add a comment |Â
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)
add a comment |Â
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)
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)
edited Jun 2 at 15:38
answered Jun 3 '16 at 2:23
èçñþçèçèç
1,6661429
1,6661429
add a comment |Â
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
answered Jun 3 '16 at 2:06
cas
38k44495
38k44495
add a comment |Â
add a comment |Â
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
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
add a comment |Â
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
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
add a comment |Â
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
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
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
add a comment |Â
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
add a comment |Â
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' charactertest with classic
[ ... ]
ortest ...
and"$var"
(becausestat %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 subshellsome 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
add a comment |Â
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' charactertest with classic
[ ... ]
ortest ...
and"$var"
(becausestat %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 subshellsome 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
add a comment |Â
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' charactertest with classic
[ ... ]
ortest ...
and"$var"
(becausestat %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 subshellsome 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
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' charactertest with classic
[ ... ]
ortest ...
and"$var"
(becausestat %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 subshellsome 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
answered Jun 4 '16 at 8:54
dave_thompson_085
2,0071810
2,0071810
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
shellcheck will report some warnings, if that's what you're asking about....
â drewbenn
Jun 4 '16 at 6:42