fswatch – Linux에서 파일 및 디렉터리 변경 또는 수정 모니터링

fswatch는 지정된 파일이나 디렉토리의 내용이 변경되거나 수정될 때 알림을 받는 크로스 플랫폼 파일 변경 모니터입니다.

다음과 같은 다양한 운영 체제에서 네 가지 유형의 모니터를 실행합니다:

  1. A monitor build on the File System Events API of Apple OS X.
  2. A monitor based on kqueue, a notification interface present in FreeBSD 4.1 also supported on many *BSD systems, OS X inclusive.
  3. A monitor based on File Events Notification API of the Solaris kernel plus its spin-offs.
  4. A monitor based on inotify, a kernel subsystem that shows file system modifications to apps.
  5. A monitor based on ReadDirectoryChangesW, a Windows API that records alters to a directory.
  6. A monitor that regularly check that status of file system, keeps file modification times in memory, and manually determine file system changes (which works anywhere, where stat can be used).

  1. fswatch의 특징
  2. 여러 OS별 API를 지원합니다
  3. 재귀적 디렉토리 모니터링을 허용합니다
  4. 포함 및 제외 정규 표현식을 사용한 경로 필터링을 수행합니다
  5. 사용자 정의 가능한 레코드 형식을 지원합니다

Linux 시스템에 fswatch 설치하는 방법

안타깝게도, 어떤 Linux 배포판에서도 기본 시스템 저장소에서 fswatch 패키지를 설치할 수는 없습니다. 최신 버전의 fswatch를 설치하는 유일한 방법은 다음 설치 지침에 표시된대로 소스 tarball에서 빌드하는 것입니다.

먼저 다음 fswatch tarball을 다운로드하고 다음과 같이 설치합니다:

$ wget https://github.com/emcrisostomo/fswatch/releases/download/1.9.3/fswatch-1.9.3.tar.gz
$ tar -xvzf fswatch-1.9.3.tar.gz
$ cd fswatch-1.9.3
$ ./configure
$ make
$ sudo make install 

중요: fswatch를 소스에서 컴파일하기 전에 시스템에 GNU GCC (C 및 C++ 컴파일러) 및 개발 도구 (build-essentialDebian/Ubuntu에서)가 설치되어 있는지 확인하십시오. 그렇지 않은 경우, 각각의 Linux 배포판에서 다음 명령을 사용하여 설치하십시오.

# yum group install 'Development Tools'		[On CentOS/RHEL]
# dnf group install 'Development Tools'		[On Fedora 22+ Versions]
$ sudo apt-get install build-essential          [On Debian/Ubuntu Versions]

Debian/Ubuntu 배포판에서 fswatch 명령을 실행하는 동안 다음 오류가 발생할 수 있습니다..

fswatch: error while loading shared libraries: libfswatch.so.6: cannot open shared object file: No such file or directory

이를 해결하려면 아래 명령을 실행해야 합니다. 이는 동적 라이브러리의 링크와 캐시를 새로 고침하여 fswatch를 사용하기 전에 도와줍니다.

$ sudo ldconfig

Linux에서 fswatch를 어떻게 사용하나요?

fswatch를 실행하는 일반적인 구문은 다음과 같습니다:

$ fswatch [option] [path]

Linux에서는 기본 inotify 모니터를 사용하는 것이 좋습니다. -M 또는 - list-monitors 옵션을 사용하여 사용 가능한 모니터를 나열할 수 있습니다:

$ fswatch -M
$ fswatch --list-monitors
fswatch – List Monitors

아래 명령은 현재 디렉토리(/home/tecmint)의 변경 사항을 감시하고, 이벤트가 4초마다 표준 출력으로 전달됩니다.

-l 또는 –-latency 옵션을 사용하여 지연 시간을 초 단위로 설정할 수 있으며, 기본값은 1초입니다.

$ fswatch -l 4 . 

다음 명령은 /var/log/auth.log 파일의 변경 사항을 5초마다 모니터링합니다:

$ fswatch -l 5 /var/log/auth.log

-t 또는 --timestamp 옵션을 사용하면 각 이벤트에 대한 타임 스탬프를 출력하며, -u 또는 --utf-time 옵션을 사용하여 UTC 형식으로 시간을 출력할 수 있습니다. 또한 -f 또는 --format-time 형식 옵션을 사용하여 시간을 형식화할 수 있습니다:

$ fswatch --timestamp /var/log/auth.log

그다음, -x 또는 --event-flagsfswatch가 이벤트 경로와 함께 이벤트 플래그를 출력하도록 지시합니다. 특정 구분자를 사용하여 이벤트를 출력하려면 –event-field-seperator 옵션을 사용할 수 있습니다.

$ fswatch --events-flags ~ /var/log/auth.log

홈 디렉토리와 /var/log/auth.log 파일의 변경 사항을 나타내는 이벤트의 숫자 값을 인쇄하려면 다음과 같이 -n 또는 --numeric 옵션을 사용하십시오:

$ fswatch --numeric ~ /var/log/auth.log 

상세한 사용 옵션 및 정보를 위해 fswatch 맨 페이지를 확인할 수 있습니다:

$ man fswatch

자세한 정보 및 사용법은 fswatch Github 저장소를 방문하세요: https://github.com/emcrisostomo/fswatch

이 게시물에서는 지정된 파일이나 디렉터리 계층의 내용이 수정될 때 Linux 사용자에게 알림을 제공하는 간단한 명령 줄 유틸리티를 다루었습니다.

I hope all went well with the installation, if that is not the case for you, make an effort to reach us via the feedback form below. In addition, in case you have used it before, you may want to offer us some thoughts about your experience with fswatch.

Source:
https://www.tecmint.com/fswatch-monitors-files-and-directory-changes-modifications-in-linux/