Anyway to track changes of files, folders, etc

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
erie88
Level 1
Level 1
Posts: 29
Joined: Mon Dec 31, 2012 11:24 am

Anyway to track changes of files, folders, etc

Post 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.
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29597
Joined: Wed Jul 06, 2011 3:58 am

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

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

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

Post 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.
User avatar
xenopeek
Level 25
Level 25
Posts: 29597
Joined: Wed Jul 06, 2011 3:58 am

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

Post 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 &
Image
Locked

Return to “Scripts & Bash”