
作為我日常開發任務的一部分(在運行 OS 10.12.3 的 Mac 上),我tail -f *
從終端機在日誌資料夾中運行。該資料夾包含大約 15 個不同的檔案。我如何更改此命令來監視更改除一個文件外的所有文件從*
?假設我想要排除的唯一檔案*
名為Repetitive.log
.
對於這個非常基本的問題表示歉意,我環顧四周,沒有看到重複的問題。轉貼自https://stackoverflow.com/questions/42815599/exclude-files-from-the-catchall-symbol
答案1
如果您使用bash
shell,請將環境變數設定GLOBIGNORE
為您想要的以冒號分隔的模式列表不是當 shell 通配時匹配,例如
$ export GLOBIGNORE=Repetitive.log
$ export GLOBIGNORE=somefile:anotherfile:abc*
從man bash
:
GLOBIGNORE
A colon-separated list of patterns defining the set of
filenames to be ignored by pathname expansion. If a file-
name matched by a pathname expansion pattern also matches
one of the patterns in GLOBIGNORE, it is removed from the
list of matches.
答案2
xargs
是你的朋友!如果沒有,find
也可以提供協助。
xargs
以下是使用或find ... -exec
擴展模式匹配的四種方法:
使用xargs
通過ls
和grep
ls | grep -v Repetitive.log | xargs tail -f
使用xargs
透過find
find . -maxdepth 1 ! -name Repetitive.log | xargs tail -f
find
與-exec
參數一起使用
find . -maxdepth 1 ! -name Repetitive.log -exec tail -f {} \;
使用擴充模式匹配bash
很好的答案,摘自https://stackoverflow.com/a/19429723/1862762。
shopt -s extglob
tail -f /directory/of/logfiles/!(Repetitive.log)
筆記
對於此任務,我更喜歡這種xargs
方式,因為它提供了tail
以相應檔案名稱標記的輸出。使用ls
andgrep
似乎更直觀、更容易記住。