Wednesday, January 10, 2018

How To Monitor File Changes Using fswatch In Linux

https://www.ostechnix.com/monitor-file-changes-using-fswatch-linux


Monitor File Changes Using fswatch In Linux
The fswatch is a free, open source multi-platform file change monitor utility that receives notifications when the contents of the specified files or directories are modified or changed. Using fswatch, we can easily monitor the changes being made in files or directories. It supports all operating systems, including GNU/Linux, *BSDs, Mac OS X, Solaris, and Microsoft Windows etc. In this brief guide, let me show you how to monitor files changes using fswatch in Unix-like operating systems.

Types of monitoring

fswatch implements the following types of monitors.
  • A monitor based on the File System Events API of Apple OS X.
  • A monitor based on kqueue, a notification interface introduced in FreeBSD 4.1.
  • A monitor based on the File Events Notification API of the Solaris kernel and its derivatives.
  • A monitor based on inotify, a Linux kernel subsystem that reports file system changes to applications.
  • A monitor based on ReadDirectoryChangesW, a Microsoft Windows API that reports changes to a directory.
  • A monitor which periodically stats the file system, saves file modification times in memory, and manually calculates file system changes.

Features

Concerning about the features, we can list the following:
  • Cross-platform and open source utility.
  • Support for many OS-specific APIs.
  • Recursive directory monitoring.
  • Path filtering using including and excluding regular expressions.
  • Customizable record format.
  • Support for periodic idle events.
  • And many.

Monitor File Changes Using fswatch In Linux

The fswatch utility is not available in the default repositories of any Linux distributions. You need to manually compile and install it from the source.
Before compiling, you need to install Development tools in your Linux distribution. To install Development tools on various Linux distributions, refer the following guide.
Then, download the fswatch source file from here.
wget https://github.com/emcrisostomo/fswatch/releases/download/1.9.3/fswatch-1.9.3.tar.gz
Extract the downloaded tarball:
tar -zxvf fswatch-1.9.3.tar.gz
Go to the project’s folder:
cd fswatch-1.9.3/
Finally, compile and install fswatch by running the following commands one by one.
./configure
make
sudo make install
Finally, run the following command to refresh the links and cache to the dynamic libraries:
sudo ldconfig
If you don’t run the above command, you might get the following error in GNU/Linux systems.
fswatch: error while loading shared libraries: libfswatch.so.6: cannot open shared object file: No such file or directory

fswatch usage

Usage of fswatch is no big deal. The typical syntax of fswatch is:
fswatch [options] ... path-0 ... path-n
To test how fswatch works, open two Terminal windows. In one Terminal (Let us call it Terminal 1), run the fswatch command to monitor the $HOME directory.
fswatch /home/sk/
And, in the other Terminal window (Terminal 2) do some operations such as creating files/folders, deleting files, modifying files etc.
Whatever you do in the terminal 2 will be notified on the Terminal 1. Have a look at the following screenshots.
Terminal 1 – fswatch command is running and the file changes are being monitored:

Terminal 2 – Do some random changes in files/folders:

By default fswatch will choose the best monitor available on the current platform, in terms of performance and resource consumption. In Linux, the default monitor is inotify.
To list the available monitors in the current platform (i.e Linux in our case), run:
fswatch -M
Or,
fswatch --list-monitors
Sample output:
inotify_monitor
poll_monitor
To monitor a specific file or directory with a particular monitor option, run:
swatch -m kqueue_monitor /home/sk/
By default, fswatch will keep monitoring the file changes until you manually stopped it by invoking CTRL+C command. Also, You can exit fswatch after the first set of events is received by specifying the option -1 as shown in the following command:
fswatch -1 /home/sk/
This command will exit just after the first set of events is received.
fswatch will monitor changes in all files/folders in the specified path. If you want to watch the changes made in the directories only, use -d option.
fswatch -d /home/sk/
Of course, there are more options. Refer the man pages or the project’s documentation page for detailed instructions.
man fswatch
And, that’s all for now. I will be soon here with another useful guide. Till then, stay tuned with OSTechNix.
Cheers!
Resource:

No comments:

Post a Comment