Linux中文件更改時如何執行命令?
已發表: 2022-04-04如果您想了解如何在工作目錄中的任何文件更改以及創建新文件時運行 Linux 命令,那麼您來對地方了。
在 Linux 中,您可以使用cron
來安排命令在特定時間運行。
但是,如果每次修改文件或將新文件添加到目錄時都需要運行命令怎麼辦?
這也很容易實現,並且有一些命令行工具可以執行此操作。
讓我們開始吧!
手錶執行器
watchexec
是一個方便且獨立的工具,它監視給定的工作目錄並在檢測到文件更新或新文件創建等任何更改時執行命令。
特徵
- 它不需要涉及
xargs
(擴展參數)的複雜命令行。 - 實時檢查當前目錄和所有子目錄的變化。
- 不需要語言運行時,它不連接到任何特定的語言或生態系統。
- 默認使用.gitignore和.ignore來決定忽略哪些文件的通知。
- 進程組用於跟踪分叉程序。
- 支持觀看具有特定擴展名的文件。
- 它與 OS X、Linux 和 Windows 兼容。
- 基於 glob 過濾和忽略事件(用於查找部分相同文件名的文件搜索模式是可能的。
安裝watchexec
要輕鬆安裝watchexec
工具,請將以下鏈接粘貼到終端或 shell 提示符中,然後按 enter。
Linux 和 macOS
curl -sS https://webinstall.dev/watchexec | bash
安裝成功後,路徑會顯示在屏幕上。 就我而言,此工具安裝在/root/.local/bin
┌──(rootkali)-[~] └─# curl -sS https://webinstall.dev/watchexec | bash Thanks for using webi to install 'watchexec@stable' 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 工具一起使用。
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 ” 代表Event Notify Test Runne r。 創建此工具的目的是進行快速反饋和自動化測試。
安裝
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 中刪除文件和目錄感興趣。