如何對目前目錄中的所有日誌檔案(除了一個日誌檔案)執行“tail -f”?

如何對目前目錄中的所有日誌檔案(除了一個日誌檔案)執行“tail -f”?

作為我日常開發任務的一部分(在運行 OS 10.12.3 的 Mac 上),我tail -f *從終端機在日誌資料夾中運行。該資料夾包含大約 15 個不同的檔案。我如何更改此命令來監視更改除一個文件外的所有文件*?假設我想要排除的唯一檔案*名為Repetitive.log.

對於這個非常基本的問題表示歉意,我環顧四周,沒有看到重複的問題。轉貼自https://stackoverflow.com/questions/42815599/exclude-files-from-the-catchall-symbol

答案1

如果您使用bashshell,請將環境變數設定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通過lsgrep

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以相應檔案名稱標記的輸出。使用lsandgrep似乎更直觀、更容易記住。

相關內容