Skip to content

Instantly share code, notes, and snippets.

@bagonyi
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bagonyi/c5ff8a3e342735023d8c to your computer and use it in GitHub Desktop.
Save bagonyi/c5ff8a3e342735023d8c to your computer and use it in GitHub Desktop.
Audits a directory and logs changes. It is a copy & paste of Watchdog's example: http://pythonhosted.org//watchdog/quickstart.html#a-simple-example. Requirements: Python 2.6+, Watchdog (pip install watchdog). Installation: add audit-dir to your PATH and make it executable (chmod +x audit-dir). Usage: cd to the dir you want to audit and run audit…
#!/usr/bin/python
# Example output:
#
# 2015-03-02 14:23:57 - Created directory: ./test
# 2015-03-02 14:23:57 - Modified directory: .
# 2015-03-02 14:25:21 - Created file: ./test/a.txt
# 2015-03-02 14:25:21 - Modified directory: ./test
# 2015-03-02 14:25:21 - Modified file: ./test/a.txt
# 2015-03-02 14:25:41 - Moved file: from ./test/a.txt to ./test/b.txt
# 2015-03-02 14:25:41 - Modified directory: ./test
# 2015-03-02 14:26:05 - Modified file: ./test/b.txt
# 2015-03-02 14:26:17 - Created file: ./test/c.txt
# 2015-03-02 14:26:17 - Modified directory: ./test
# 2015-03-02 14:26:17 - Modified file: ./test/c.txt
# 2015-03-02 14:26:24 - Deleted file: ./test/c.txt
# 2015-03-02 14:26:24 - Modified directory: ./test
import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
path = sys.argv[1] if len(sys.argv) > 1 else '.'
event_handler = LoggingEventHandler()
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment