Windows 上是否有 tail -f 的等价物?

Is there an equivalent of tail -f on Windows?

很多时候,我发现自己不得不关注 Windows 上的日志文件的演变。是否有等同于 Linux

tail -f <filename>

在 Windows 终端上执行命令,最好无需安装外部软件?其他 SO 帖子谈论安装第三方程序。

在 Powershell 中,您可以使用 Get-Content 和 -Wait 标志:

Get-Content filename.log -Wait

您可以将 Get-Content 缩短为 gc。建议作为可能重复的问题有一个答案,其中提到了这个和一些有用的额外参数 - 参见 。不过,我不确定它是否真的重复,因为这个问题是在谈论 Linux tail 的一般 Windows 替代方案,而不是 tail -f.

是的。您可以在 windows 上使用 tail,这是在 windows 和 tail 上访问大量 GNU 工具的一个很小的代价。因为它与 git for windows 捆绑在一起,所以它经过严格测试且稳定。

首先从 https://gitforwindows.org/

安装 git-bash

接下来,将 git-bash 放在 windows 路径上并重新启动您的工作站:

setx path "%path%;C:\Program Files\Git\bin\"

现在,您应该可以使用 tail -n 20 -F logging_file.log 尾部任何文件并显示最后 20 行。

如果您在 Linux/Unix 上并且想要持续查看日志,您可以使用以下命令: ssh username@10.15.3.3 'bash -c "tail -n 20 -F /c/Users/username/Desktop/logging_file.log"'

在 Powershell 中使用:

cat .\<file_name> -Tail 10 -Wait

我知道你说没有外部程序。但是对于已经为 Linux (WSL) 安装了 Windows 子系统的人来说,他们无法使 tail 在 [=20= 中正常工作] 16.04 LTS 我发现 this thread 有人找到了解决方法:

In case anyone finds this through Google, it seems that inotify support in WSL is limited to WSL file accesses, not win32 file accesses, so you have to tell tail not to use it:

tail -f /mnt/c/path/to/file ---disable-inotify

(yes, three dashes)

Get-Content 文件名 -Wait -tail 1

正如 nikobelia 所说,这对我有用,只是添加了 tail 选项,它按预期工作!