Page 1 of 1

Anyway to track changes of files, folders, etc

Posted: Sun Feb 10, 2013 12:44 am
by erie88
I don't know if it is possible but is there way to track for any changes that have been made to files, folders, and sub directory? and print the output in log file?
The reason is I have this habit of deleting files using keyboard shortcut "shift+delete" so I have the need for a log file.

I tried searching on Google and found this one here : http://linuxos4all.blogspot.com/2010/11 ... -been.html. I'm pretty much newbie so I'm not sure how. I would really appreciate if someone could help me explaining how to or step-by-step.

Thanks.

Re: Anyway to track changes of files, folders, etc

Posted: Sun Feb 10, 2013 3:45 am
by xenopeek
Moved to the scripting forum.

I have done something similar before. You'll need to install the package inotify-tools.

You would create a file somewhere in your home folder, put the following into it, mark the file executable, and in the Startups Applications program add the full path to the file to run it at login. It keeps running once started, and creates a log file in your home folder called 'deleted-files' (you can configure that below).

Code: Select all

#!/bin/bash

# configure logfile
LOGFILE=~/deleted-files

# watch home folder for deleted files and folders
do_watch () {
	while true; do
		NAME=$(inotifywait --recursive --quiet --event delete --format '%w%f' ~)
		echo "$(date +'%b %d %X'): $NAME" >> "$LOGFILE"
	done
}

# start watching
do_watch &
Now, here comes the annoying bit. This isn't perfect; it will log any file that is deleted and not only yours. And perhaps when you delete a batch of files in one go, it may not catch all events of that...

Re: Anyway to track changes of files, folders, etc

Posted: Sun Feb 10, 2013 8:50 pm
by erie88
thanks. Can this also works with files on media like external hard drives? I noticed this only record changes in home as I put the script in home folder.

Re: Anyway to track changes of files, folders, etc

Posted: Mon Feb 11, 2013 4:01 am
by xenopeek
I don't know, you should test it. External hard disks are normally mounted to /media and not to your home folder, so those aren't included in the watch. You could add the following pieces of code to the file and it will also watch /media:

Code: Select all

# watch /media for deleted files and folders
do_watch_media () {
   while true; do
      NAME=$(inotifywait --recursive --quiet --event delete --format '%w%f' /media)
      echo "$(date +'%b %d %X'): $NAME" >> "$LOGFILE"
   done
}

# start watching /media
do_watch_media &