mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-29 21:41:00 +08:00
251 lines
7.5 KiB
Markdown
251 lines
7.5 KiB
Markdown
[#]: subject: "How To Monitor File Changes Using fswatch In Linux"
|
||
[#]: via: "https://ostechnix.com/monitor-file-changes-using-fswatch-linux/"
|
||
[#]: author: "sk https://ostechnix.com/author/sk/"
|
||
[#]: collector: "lkxed"
|
||
[#]: translator: " "
|
||
[#]: reviewer: " "
|
||
[#]: publisher: " "
|
||
[#]: url: " "
|
||
|
||
How To Monitor File Changes Using fswatch In Linux
|
||
======
|
||
|
||
**Fswatch** is a free, open source multi-platform file change monitor utility that notifies us 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 and/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 file changes using fswatch** in Linux and 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 of fswatch, 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.
|
||
|
||
### Install fswatch in Linux
|
||
|
||
The fswatch utility is available in the default repositories of popular Linux distributions.
|
||
|
||
To install fswatch in Debian, Ubuntu, Linux Mint, Pop OS, and other APT-based systems, run:
|
||
|
||
```
|
||
$ sudo apt install fswatch
|
||
```
|
||
|
||
To install fswatch in Fedora, RHEL, CentOS, AlmaLinux and Rocky Linux, **enable [EPEL] repository** using command:
|
||
|
||
```
|
||
$ sudo dnf install epel-release
|
||
```
|
||
|
||
And then install install fswatch using command:
|
||
|
||
```
|
||
$ sudo dnf install fswatch
|
||
```
|
||
|
||
To install fswatch in openSUSE, run:
|
||
|
||
```
|
||
$ sudo zypper install fswatch
|
||
```
|
||
|
||
#### Install fswatch from source
|
||
|
||
If fswatch is not available for your distribution, you can manually compile and install the latest version from the source as described below.
|
||
|
||
Before compiling, you need to **install Development tools** in your Linux distribution. To install Development tools on various Linux distributions, refer the following guide.
|
||
|
||
- [**How To Install Development Tools In Linux**][1]
|
||
|
||
Then, download the fswatch source file [**from here**][2].
|
||
|
||
```
|
||
$ wget https://github.com/emcrisostomo/fswatch/releases/download/1.17.1/fswatch-1.17.1.tar.gz
|
||
```
|
||
|
||
Extract the downloaded tarball:
|
||
|
||
```
|
||
$ tar -zxvf fswatch-1.17.1.tar.gz
|
||
```
|
||
|
||
Go to the project's folder:
|
||
|
||
```
|
||
$ cd fswatch-1.17.1/
|
||
```
|
||
|
||
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
|
||
```
|
||
|
||
Finally, check the fswatch version to make sure if it is installed correctly:
|
||
|
||
```
|
||
$ **fswatch --version**
|
||
fswatch 1.17.1
|
||
Copyright (C) 2013-2021 Enrico M. Crisostomo <enrico.m.crisostomo@gmail.com>.
|
||
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
|
||
This is free software: you are free to change and redistribute it.
|
||
There is NO WARRANTY, to the extent permitted by law.
|
||
|
||
Written by Enrico M. Crisostomo.
|
||
```
|
||
|
||
### Install fswatch in FreeBSD
|
||
|
||
On FreeBSD, fswatch can be installed using `pkg` as `root`:
|
||
|
||
```
|
||
# pkg install fswatch-mon
|
||
```
|
||
|
||
### Monitor file changes using fswatch in Linux
|
||
|
||
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 (Let us call them **Terminal 1** and **Terminal 2**).
|
||
|
||
In Terminal 1, run the fswatch command to monitor the **`$HOME`** directory.
|
||
|
||
```
|
||
$ fswatch /home/ostechnix/
|
||
```
|
||
|
||
And, in the Terminal 2 do some operations such as creating files/folders, deleting files, and 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:
|
||
|
||
![Monitor File Changes Using fswatch][3]
|
||
|
||
Monitor File Changes Using fswatch
|
||
|
||
**Terminal 2** - Do some random changes in files/folders:
|
||
|
||
![Do random changes in files or folders][4]
|
||
|
||
Do random changes in files or 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**.
|
||
|
||
By default, fswatch will keep monitoring the file changes until you manually stop it by invoking **`CTRL+C`** keys.
|
||
|
||
### List monitors
|
||
|
||
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
|
||
```
|
||
|
||
### Monitor specific file or folder
|
||
|
||
To monitor a specific file or directory with a particular monitor option, run:
|
||
|
||
```
|
||
$ swatch -m kqueue_monitor /home/ostechnix/
|
||
```
|
||
|
||
You can also exit fswatch after the first set of events is received by specifying the option **`-1`** as shown in the following command:
|
||
|
||
```
|
||
$ fswatch -1 /home/ostechnix/
|
||
```
|
||
|
||
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/ostechnix/
|
||
```
|
||
|
||
Of course, there are more options. Refer the man pages or the [**project's documentation page**][5] for detailed instructions.
|
||
|
||
```
|
||
$ man fswatch
|
||
```
|
||
|
||
**Resource:**
|
||
|
||
- [**fswatch GitHub Repository**][6]
|
||
|
||
Featured Image by [Pete Linforth][7] from [Pixabay][8].
|
||
|
||
--------------------------------------------------------------------------------
|
||
|
||
via: https://ostechnix.com/monitor-file-changes-using-fswatch-linux/
|
||
|
||
作者:[sk][a]
|
||
选题:[lkxed][b]
|
||
译者:[译者ID](https://github.com/译者ID)
|
||
校对:[校对者ID](https://github.com/校对者ID)
|
||
|
||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||
|
||
[a]: https://ostechnix.com/author/sk/
|
||
[b]: https://github.com/lkxed
|
||
[1]: https://ostechnix.com/install-development-tools-linux/
|
||
[2]: https://github.com/emcrisostomo/fswatch/releases
|
||
[3]: https://ostechnix.com/wp-content/uploads/2022/10/Monitor-File-Changes-Using-fswatch.png
|
||
[4]: https://ostechnix.com/wp-content/uploads/2022/10/Do-random-changes-in-files-or-folders.png
|
||
[5]: https://emcrisostomo.github.io/fswatch/doc/
|
||
[6]: https://github.com/emcrisostomo/fswatch
|
||
[7]: https://pixabay.com/users/thedigitalartist-202249/?utm_source=link-attribution&utm_medium=referral&utm_campaign=image&utm_content=3376230
|
||
[8]: https://pixabay.com//?utm_source=link-attribution&utm_medium=referral&utm_campaign=image&utm_content=3376230
|