Shell命令监视文件中的更改 – 再次调用什么?

我知道在Unix上有一个命令,我可以用它来监视一个文件,并看到正在写入它的变化。 这对于检查日志文件特别有用。

你知道它叫什么吗?

你的意思是

tail -f logfile.log 

(手册页尾 )

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

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

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

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

我更喜欢使用less +FG 1 ,因为我发现自己需要search特定错误或ID的日志文件。 如果我需要search的东西,我input^C停止关注该文件和? 开始向后search。

键绑定几乎和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. 

对于非常长的日志,我发现使用closures行号的-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). 

1.给rgmarcha的帽子提示在评论中指出这一点。

尾巴是伟大的…less,也可以使用文件开始较less,即lessmyfile,然后按F.这less有作为尾巴。

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

 inotifywait -r -m -e modify /var/log | while read path _ file; do echo $path$file modified done 

我正在编辑一个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 

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

./watch.sh file.tex pdflatex

如果你只想在命令执行时进行实际的修改,你可以使用`md5sum "$FILE"`而不是`ls -l "$FILE"`

你可以使用tailf命令这个非常简单的命令

 tailf logfile.log 

你也可以使用inotifywatch / inotifywait钩入内核inotify子系统。 这样,您还可以观察“打开”,“closures”或“访问”等内容。

但是,如果你只是想获得附加的行标准输出我同意尾巴。

尾巴是标准的,传统的,随处可用的unix工具。 一个更复杂一点的工具是multitil ,它可以同时监视多个文件并进行语法高亮显示。

如果我希望能够search文件,除了拖尾之外,我用“F”命令less用。

当使用tail时,请记住,如果文件可能被翻转或被编辑(vim的默认模式:w)取代,则需要额外的参数。

tail -f将导致尾部存储文件描述符并跟随它。 如果文件被replace,描述符将被改变。 跟随文件描述符的好处是,如果文件被重命名,你仍然会跟着它。

tail –follow =将通过定期重新打开命令来跟踪指定的文件,以查看它是否已被replace。

–retry是另一个有用的选项,如果你想尾日志文件,但该文件尚未创build。

tail -F是–follow = –retry的快捷方式。

tail -f somefile.txt不断滚动新数据时,我有时候更喜欢less +G somefile.txt以查看文件中最新数据的补丁。