Per*_*ulf 98
你可以用tail command与-f :
tail -f /var/log/syslog
Run Code Online (Sandbox Code Playgroud)
这是实时显示的好解决方案。
Vol*_*gel 26
如果您想显示一个适合一个终端屏幕的短文件,并且正在更改的可能是整个文件,您可以使用watch:
watch cat example.txt
Every 2.0s: cat example.txt Sun Aug 3 15:25:20 2014
Some text
another line
Run Code Online (Sandbox Code Playgroud)
默认情况下,它每 2 秒显示一次整个文件,包括一个可选的标题:
选项-d( --differences) 将突出显示与先前版本输出或第一个版本的更改。
小智 9
当我需要检测文件更改并执行其他tail -f filename操作时,我已inotifywait在脚本中使用来检测更改并对其采取行动。使用示例如下所示。有关man inotifywait其他事件名称和开关,请参阅。您可能需要安装该inotify-tools软件包,例如通过sudo apt-get install inotify-tools.
这是示例脚本,称为exec-on-change:
#!/bin/sh
# Detect when file named by param $1 changes.
# When it changes, do command specified by other params.
F=$1
shift
P="$*"
# Result of inotifywait is put in S so it doesn't echo
while S=$(inotifywait -eMODIFY $F 2>/dev/null)
do
# Remove printf if timestamps not wanted
printf "At %s: \n" "$(date)"
$P
done
Run Code Online (Sandbox Code Playgroud)
在两个控制台中,我输入了如下命令(其中 A> 表示控制台 A 中的条目,B> 表示控制台 B 中的条目。)
A> rm t; touch t
B> ./exec-on-change t wc t
A> date >>t
A> date -R >>t
A> date -Ru >>t
A> cat t; rm t
Run Code Online (Sandbox Code Playgroud)
以下输出cat t出现在控制台 A 中:
Thu Aug 16 11:57:01 MDT 2012
Thu, 16 Aug 2012 11:57:04 -0600
Thu, 16 Aug 2012 17:57:07 +0000
Run Code Online (Sandbox Code Playgroud)
exec-on-change控制台 B 中出现以下输出:
At Thu Aug 16 11:57:01 MDT 2012:
1 6 29 t
At Thu Aug 16 11:57:04 MDT 2012:
2 12 61 t
At Thu Aug 16 11:57:07 MDT 2012:
3 18 93 t
Run Code Online (Sandbox Code Playgroud)
该exec-on-change脚本结束时,我rm“d t。