Shell 命令监视文件中的更改

Seb*_*itz 192 linux shell

我知道 Unix 上有一个命令可以用来监视文件并查看写入文件的更改。这对于检查日志文件非常有用。

你知道它叫什么吗?

Jon*_*eet 244

你的意思是

tail -f logfile.log
Run Code Online (Sandbox Code Playgroud)

?

尾部的手册页

  • 旁注:如果您的发行版提供了 tailf 命令,请优先使用该命令而不是 tail -f。tailf 效率更高,因为如果它没有被写入,它就不需要访问被监视的文件(如果您使用 atime 更新安装文件系统,则轮询访问很烦人。) (19认同)
  • `tail -F` 将跟随文件名而不是文件对象,这在日志文件轮换的情况下特别有用。 (19认同)
  • 在 [超级用户我找到了答案](http://superuser.com/a/155214) 也推荐 _tail -F_ 而不是 _-f_ (11认同)
  • 是的,这是实时的。 (7认同)
  • 几年后更新:`tailf` 现在已弃用,`tail -f` 是安全的。(使用`man tailf`在您的系统上确认这一点。)请参阅文档:http://man7.org/linux/man-pages/man1/tailf.1.html (4认同)

Mur*_*iar 142

根据 Jon Skeet 的回答,您可能是指尾巴。

另一个有用的是watch;它允许您定期运行命令并全屏查看输出。例如:

看 -n 10 -d ls -l /var/adm/messages

ls -l /var/adm/messages每 10 秒运行一次命令,并突出显示后续运行之间输出的差异。(例如,用于观察日志文件的增长速度)。


小智 62

inotifywait如果您想在每次文件(或目录中的任何文件)更改时运行命令,则来自inotify-tools很有用。例如:

inotifywait -r -m -e modify /var/log | 
   while read path _ file; do 
       echo $path$file modified
   done
Run Code Online (Sandbox Code Playgroud)

  • 请注意,`path` 不是变量名的最佳选择。在 `zsh` 上,环境变量似乎不区分大小写。对我来说,设置 `path` 会导致 `PATH` 也被设置,这基本上意味着在你修复它之前什么都不会执行。在`bash`上,设置`path`对`PATH`没有影响。 (4认同)
  • @Thanatos Zsh 变量*区分大小写,但在 Zsh 本身设置的变量中,Zsh 将 `*PATH` 变量“绑定”到同名但 **小写** 的 **array**。绑定变量总是由一个标量和一个数组组成(例如`PATH` 和`path`),修改一个会修改另一个。一个关键特性是数组版本在标量版本(`:`)中的分隔符上自动拆分。自己看看`print "$PATH\n$path"`。`zshparam(1)` 手册页的 `PARAMETERS USED BY THE SHELL` 部分的第二段有更详细的信息。 (2认同)

Jon*_*son 38

我更喜欢使用less +FG1tail -f因为我发现自己需要在日志文件中搜索特定错误或 ID。如果我需要搜索某些内容,我会输入^C以停止跟踪文件并?开始向后搜索。

键绑定与vi. 可以使用以下+选项在启动时初始化任何命令:

+cmd   Causes  the  specified  cmd  to be executed each time a new file is
       examined.  For example, +G causes less to  initially  display  each
       file starting at the end rather than the beginning.
Run Code Online (Sandbox Code Playgroud)

对于非常长的日志,我发现使用-n关闭行编号的选项很方便。从联机帮助页:

-n or --line-numbers
          Suppresses line numbers.  The default (to use line numbers)  may
          cause  less  to run more slowly in some cases, especially with a
          very large input file.  Suppressing line  numbers  with  the  -n
          option  will  avoid this problem.  Using line numbers means: the
          line number will be displayed in the verbose prompt and in the =
          command,  and the v command will pass the current line number to
          the editor (see also  the  discussion  of  LESSEDIT  in  PROMPTS
          below).
Run Code Online (Sandbox Code Playgroud)

1.感谢 rgmarcha在评论中指出这一点。


小智 21

Tail 很棒... less 也可以用于 start less 文件,即 less myfile 然后按Shift+ F。这较少充当尾巴。

  • less +F myfile 也会成功 (5认同)

小智 18

我正在编辑一个 LaTeX 文件,并希望监视它是否在中间的某个地方发生变化。我编写了以下证明对我有用的小 shell 脚本。我希望它也能对其他人派上用场。

#!/bin/bash
FILE="$1"
CMD="$2"
LAST=`ls -l "$FILE"`
while true; do
  sleep 1
  NEW=`ls -l "$FILE"`
  if [ "$NEW" != "$LAST" ]; then
    "$CMD" "$FILE"
    LAST="$NEW"
  fi
done
Run Code Online (Sandbox Code Playgroud)

将其另存为watch.sh并执行chmod u+x watch.sh。然后我按如下方式执行它:

./watch.sh file.tex pdflatex

如果您只想在发生实际修改时运行该命令,则可以使用`md5sum "$FILE"`代替`ls -l "$FILE"`.


小智 8

您可以使用 最简单的tailf命令

tailf logfile.log
Run Code Online (Sandbox Code Playgroud)


Mar*_*tin 6

您还可以使用 inotifywatch/inotifywait 挂钩到内核 inotify 子系统。通过这种方式,您还可以查看“打开”、“关闭”或“访问”等内容。

但是,如果您只是想将附加行添加到标准输出,我同意尾部。