Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check latest file updates in directory on linux using bash shell scripting

I have basic knowledge of linux bash shell scripting, right now I am facing a problem that is like following:

  1. Suppose I am working in an empty directory mydir
  2. Then there is a process which is created by a C program to generate a file with one word. (Exp: file.txt would have one word, "hello")
  3. Routinely, after a specific period of time, the file is updated by the C program with the same one word "hello".
  4. I want check the file every time when it is updated.
  5. But the issue is that I also want my script doing some other operation while checking the file updates and when it detects file updates that it returns something for which I can use to trigger something else.

So, can anyone help me.

Also, some proof of concept :

while true;
do 
    func1();
    func2();
    check file is updated or not 
    if updated ; then
       break;
     else
      continue;
like image 652
satinder Avatar asked Feb 16 '26 01:02

satinder


1 Answers

You probably want the stat command. Do man stat to see how yours works. You want to look for "modtime" or "time of last data modification" option. For mine that would be stat -c%Y file. Something like basemodtime=$(stat -c%Y file) before the loop, modtime=$(stat -c%Y file) after func2(), and then if [ $modtime != $basemodtime ]; then to detect "updated".

like image 76
Jeff Y Avatar answered Feb 19 '26 04:02

Jeff Y



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!