Linux中文件更改时如何执行命令?

如果你想知道如何在工作目录中监测文件变动,例如修改或新增文件时自动执行 Linux 命令,那么这篇文章正是为你准备的。

在 Linux 系统中,我们通常使用 cron 来定时执行命令。

但是,如果你的需求是,每当文件被修改或者有新文件被添加到目录时,就立即运行特定命令,该怎么办呢?

别担心,这完全可以实现,而且有一些命令行工具可以帮助你轻松完成这项任务。

让我们一起来看看吧!

Watchexec 工具

Watchexec 是一个功能强大的独立工具,它可以监控指定的工作目录,并在检测到文件更新或新文件创建等任何变化时,自动执行预设的命令。

主要特点

  • 无需复杂的命令行操作,避免使用 xargs(扩展参数)。
  • 实时监控当前目录及其所有子目录的变化。
  • 无需特定的语言运行时环境,与任何特定语言或生态系统无关。
  • 默认使用 .gitignore 和 .ignore 文件来决定哪些文件变动应该被忽略。
  • 利用进程组来管理衍生程序。
  • 支持监控特定扩展名的文件。
  • 兼容 OS X、Linux 和 Windows 等多种操作系统。
  • 基于 glob 模式过滤和忽略事件,可以实现对文件名部分匹配的文件进行搜索。

Watchexec 的安装

要安装 watchexec 工具,只需将以下命令复制粘贴到你的终端或 shell 提示符中,然后按下回车键。

针对 Linux 和 macOS 系统:

curl -sS https://webinstall.dev/watchexec | bash

安装成功后,路径信息将会显示在屏幕上。 例如,在我的情况下,该工具安装在 /root/.local/bin 目录下。

  ┌──(root💀kali)-[~]
  └─# curl -sS https://webinstall.dev/watchexec | bash

  Thanks for using webi to install '[email protected]' on 'Linux/x86_64'.
  Have a problem? Experience a bug? Please let us know:
          https://github.com/webinstall/webi-installers/issues

  Lovin' it? Say thanks with a Star on GitHub:
          https://github.com/webinstall/webi-installers

  Found /root/Downloads/webi/watchexec/cli-v1.18.9/watchexec-1.18.9-x86_64-unknown-linux-musl.tar.xz
  Extracting /root/Downloads/webi/watchexec/cli-v1.18.9/watchexec-1.18.9-x86_64-unknown-linux-musl.tar.xz
  Installing to /root/.local/opt/watchexec-vcli-v1.18.9/bin/watchexec
  Installed 'watchexec vcli-v1.18.9' as /root/.local/bin/watchexec

安装完毕后,请进入 .local/bin 目录,并使用以下命令检查文件是否存在。

cd .local/bin
ls
  

接下来,你需要导出 watchexec 工具的路径,以便系统能够找到并执行它。

export PATH="/root/.local/bin:$PATH"

现在,你可以通过在终端中以 root 用户身份输入 watchexec 来运行该工具。使用 `watchexec –help` 命令可以查看 watchexec 工具的所有可用标志和选项。

watchexec --help

命令示例

  • 当目录或其子目录中的任何文件发生更改时,执行 `ls -la` 命令。 此命令会在每次检测到文件变动时显示当前目录中的所有文件。
watchexec -- ls -la
  • 当当前目录中的任何 python、js、CSS 或 HTML 扩展名的文件发生修改时,执行指定的命令。 你可以在这里传递任何你想要的命令。 文件扩展名之间应使用逗号分隔。
watchexec --exts py,js,css,html <command>
  • 当 lib 或 src 目录中的任何文件发生更改时,执行指定的命令。 使用 `-w` 选项来监控特定的文件或目录。
watchexec -w lib -w src <command>
  • 当当前目录(及其所有子目录)中的任何文件发生更改时,调用或重启任何服务。
watchexec -e html -r tor

watchexec -e js,py -r mysql

在这里,如果进程或服务正在系统中运行,则 `-r` 选项会重新启动它。

如果需要更多关于 watchexec 的使用示例,可以访问其官方 GitHub 存储库

entr 工具

entr 是一个简单而强大的命令行实用程序,它可以在指定目录中发生任何文件变动时执行任意命令。

“entr” 代表事件通知测试运行程序,该工具的创建目的是为了实现快速反馈和自动化测试。

安装方法

entr 工具通常预装在许多 Linux 发行版中。如果你的系统中没有预装,你需要手动安装。

该工具使用简便,可以通过以下命令安装:

sudo apt-get install entr

或者,你也可以通过克隆官方 Git 存储库 来进行安装。

git clone https://github.com/eradman/entr.git

然后,进入克隆下来的目录,并使用以下命令安装必要的组件:

./configure
make test
make install

要查看可用的构建选项,请运行 `./configure -h` 命令。

命令示例

要查看 entr 命令的可用选项和参数,请使用以下命令:

man entr

此命令会显示 entr 命令的用户手册。

NAME
     entr — run arbitrary commands when files change

SYNOPSIS
     entr [-acdnprsz] utility [argument /_ ...]

DESCRIPTION
     A list of files provided on standard input, and the utility is executed using the supplied arguments
     if any of them change.  entr waits for the child process to finish before responding to subsequent
     file system events.  A TTY is also opened before entering the watch loop in order to support interac‐
     tive utilities.

     The arguments are as follows:

     -a      Respond to all events which occur while the utility is running.  Without this option, entr
             consolidates events in order to avoid looping.  This option has no effect in conjunction with
             the -r flag.

     -c      Clear the screen before invoking the utility specified on the command line.  Specify twice to
             erase the scroll back buffer.

     -d      Track the directories of regular files provided as input and exit if a new file is added.
             This option also enables directories to be specified explicitly.  If specified twice, all new
             entries to a directory are recognized, otherwise files with names beginning with ‘.’ are ig‐
             nored.

     -n      Run in non-interactive mode.  In this mode entr does not attempt to read from the TTY or
             change its properties.

     -p      Postpone the first execution of the utility until a file is modified.

 Manual page entr(1) line 1 (press h for help or q to quit)
  • 当工作目录中的任何 JavaScript 文件发生更改时,启动并自动重新加载 MySQL 服务器。每次将更改保存到文件时,entr 都会重新加载 MySQL 服务器。
ls *.js | entr -r mysql
  • 自动重新加载 Web 服务器,或者在服务器退出时终止。
$ ls * | entr -rz ./httpd

想要了解更多关于 entr 命令的详细信息和示例,请访问其官方 GitHub 存储库

结语

希望这篇文章能够帮助你学习如何在指定的目录中,当文件发生变化或创建新文件时,自动执行 Linux 命令。

此外,你可能还会对如何在 Linux 中删除文件和目录的内容感兴趣。