satinder
satinder

Reputation: 77

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;

Upvotes: 3

Views: 741

Answers (1)

Jeff Y
Jeff Y

Reputation: 2466

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".

Upvotes: 2

Related Questions