Anyway to track changes of files, folders, etc

Forum rules
Before you post please read how to get help

Anyway to track changes of files, folders, etc

Postby erie88 on Sun Feb 10, 2013 12:44 am

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/how-to-track-which-files-have-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.
erie88
Level 1
Level 1
 
Posts: 29
Joined: Mon Dec 31, 2012 11:24 am

Linux Mint is funded by ads and donations.
 

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

Postby xenopeek on Sun Feb 10, 2013 3:45 am

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...
User avatar
xenopeek
Level 21
Level 21
 
Posts: 15489
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

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

Postby erie88 on Sun Feb 10, 2013 8:50 pm

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.
erie88
Level 1
Level 1
 
Posts: 29
Joined: Mon Dec 31, 2012 11:24 am

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

Postby xenopeek on Mon Feb 11, 2013 4:01 am

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 &
User avatar
xenopeek
Level 21
Level 21
 
Posts: 15489
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands


Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 1 guest