Logging access to files and directories
Logging of file and directory access is very helpful to keep a track of changes that are happening to files and folders. This recipe will describe how to log such accesses.
Getting ready
The inotifywait
command can be used to gather information about file accesses. It doesn't come by default with every Linux distro. You have to install the inotify-tools
package by using a package manager. It also requires the Linux kernel to be compiled with inotify
support. Most of the new GNU/Linux distributions come with inotify
enabled in the kernel.
How to do it...
Let's walk through the shell script to monitor the directory access:
#/bin/bash #Filename: watchdir.sh #Description: Watch directory access path=$1 #Provide path of directory or file as a rgument to script inotifywait -m -r -e create,move,delete $path -q
A sample output is as follows:
$ ./watchdir.sh . ./ CREATE new ./ MOVED_FROM new ./ MOVED_TO news ./ DELETE news
How it works...
The previous script...