0

How to list modified or newly created files or directories in linux. So that I can trigger another command or shell script for another task. for eg. A file a.txt and test.txt are modified and I want to find the lasted changed files using linux cmd and then trigger a restart.sh script(say) to get changes.

0

There are a number of ways you can achieve this, but here is an example in Python:

#!/usr/bin/env python

import os
import glob
import time
import subprocess

if __name__ == '__main__':
    mtimes = dict()

    while True:
        for f in glob.glob('*.txt'):
            if f not in mtimes:
                mtimes[f] = os.path.getmtime(f)

            if mtimes[f] != os.path.getmtime(f):
                print("File {} was modified. Executing restart.sh.".format(f))
                mtimes[f] = os.path.getmtime(f)                
                output = subprocess.check_output('./restart.sh')
                print(output)

        time.sleep(1)

You probably need to adjust it to your environment:

  • glob.glob('*.txt') is the search pattern for the files you wish to monitor.
  • subprocess.check_output('./restart.sh') is where you call the script. It may reside somewhere else.
  • time.sleep(1) is the delay (in seconds) between checks.
0

I'd go with inotifywait. They look at the system calls for create,modify,delete,move and you can script in the shell what to do when it is detected

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.