Windows的Unix tail命令的等效项

A Windows equivalent of the Unix tail command

我正在寻找与Unix" tail"命令等效的命令,该命令将允许我在写入日志文件时查看其日志输出。


如果您使用PowerShell,则可以使用:

1
Get-Content filenamehere -Wait -Tail 30

从下方发布Stefan的评论,这样人们就不会错过

PowerShell 3引入了-Tail参数,仅包含最后x行


我建议为Win32安装类似GNU Utilities的工具。它具有最喜欢的食物,包括尾巴。


我一直在Windows中使用Baretail进行跟踪。它是免费的,非常不错。

编辑:有关Baretail的详细说明,请参见此问题


您可以将尾巴作为Cygwin的一部分。


使用批处理命令对 DOS CMD尾部感兴趣的任何人(请参见下文)。

这不是完美的,有时会重复行。

用法:tail.bat -d
tail.bat -f -f

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
@echo off
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
rem tail.bat -d <lines> <file>
rem tail.bat -f <file>

rem ****** MAIN ******
IF"%1"=="-d" GOTO displayfile
IF"%1"=="-f" GOTO followfile

GOTO end

rem ************
rem Show Last n lines of file
rem ************

:displayfile
SET skiplines=%2
SET sourcefile=%3

rem *** Get the current line count of file ***
FOR /F"usebackq tokens=3,3 delims=" %%l IN (`find /c /v"" %sourcefile%`) DO (call SET find_lc=%%l)

rem *** Calculate the lines to skip
SET /A skiplines=%find_lc%-!skiplines!

rem *** Display to screen line needed
more +%skiplines% %sourcefile%

GOTO end

rem ************
rem Show Last n lines of file & follow output
rem ************

:followfile
SET skiplines=0
SET findend_lc=0
SET sourcefile=%2

:followloop
rem *** Get the current line count of file ***
FOR /F"usebackq tokens=3,3 delims=" %%l IN (`find /c /v"" %sourcefile%`) DO (call SET find_lc=%%l)
FOR /F"usebackq tokens=3,3 delims=" %%l IN (`find /c /v"" %sourcefile%`) DO (call SET findend_lc=%%l)

rem *** Calculate the lines to skip
SET /A skiplines=%findend_lc%-%find_lc%
SET /A skiplines=%find_lc%-%skiplines%

rem *** Display to screen line when file updated
more +%skiplines% %sourcefile%

goto followloop

:end


有很多选项,但是所有选项都具有更高级功能的缺陷。

  • Windows Server 2003工具提供了一个简单的尾巴,可以通过Resource Kit Tools下载该尾巴。它在很多方面都太受限制(文件后跟随锁,缺少--pid等许多选项),但是可以完成跟踪文件的基本任务。

  • GnuWin32的尾巴有错误(αβγ)-像-f这样的东西根本不起作用。

  • UnxUtils的尾部似乎更好(-f可以工作,但--pid似乎不能,-n但--lines = n不能使-f失败),但似乎是一个无效的项目。

  • Cygwin非常丑陋,也许可以只使用DLL和coreutils包-但仍然存在--pid之类的问题,无法与本机win32进程一起使用。


我用过Windows的Tail。当然不如使用

1
tail

优雅,但是随后您使用的是Windows。 ;)


在这里的答案中,我还没有看到Log Expert。

它是可定制的,非常适合处理日志文件。到目前为止,对我来说,它是最好的Windows图形日志查看器。


如果您根本不想安装任何东西,则可以"构建自己的"批处理文件,该文件可以通过标准Windows命令执行此工作。这里有一些有关如何做的指示。

1)使用find / c / v" yourinput.file,获取输入文件中的行数。输出类似于:

1
---------- T.TXT: 15

2)使用/ f,解析此输出以获取数字15。

3)使用set / a,计算需要跳过的标题行数

4)使用/ f" skip = n"跳过头行并回显/处理尾行。

如果有时间,我将构建一个批处理文件并将其发回到此处。

编辑:tail.bat

1
2
3
4
5
6
7
8
9
10
11
12
REM tail.bat
REM
REM Usage: tail.bat <file> <number-of-lines>
REM
REM Examples: tail.bat myfile.txt 10
REM           tail.bat"C:\My File\With\Spaces.txt" 10

@ECHO OFF
for /f"tokens=2-3 delims=:" %%f in ('find /c /v"" %1') do (
    for %%F in (%%f %%g) do set nbLines=%%F )
set /a nbSkippedLines=%nbLines%-%2
for /f"usebackq skip=%nbSkippedLines% delims=" %%d in (%1) do echo %%d


使用Windows PowerShell,您可以使用:

1
Get-Content <file> -Wait

我最近使用过Mtail,它似乎运作良好。这是GUI类型,如上面提到的裸尾。
enter image description here


尝试使用Windows Services for UNIX。提供贝壳,awk,sed等以及尾巴。


从Microsoft本身下载tail命令,它是Windows Server 2003 Resource Kit Tools的一部分。


另一种选择是安装MSYS(比Cygwin的重量更轻)。


我更喜欢TailMe,因为可以在一个窗口中同时观看多个日志文件:http://www.dschensky.de/Software/Staff/tailme_en.htm


DOS的type就像* nux的cat一样工作,尽管它确实像cat一样转储了整个文件,所以它并不是真正的tail,但是在没有下载/安装文件的情况下就可以使用了。真正的tail替代。


Windows资源工具包工具包中提供了tail命令和许多其他命令。


我只是写了这个小批处理脚本。它不像Unix的" tail"那样复杂,但是希望有人可以对其进行改进,例如将输出限制为文件的最后10行,等等。如果要改进此脚本,请发送该脚本在抢劫?[at]?gmail.com时对我说。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
@echo off

:: This is a batch script I wrote to mimic the 'tail' UNIX command.
:: It is far from perfect, but I am posting it in the hopes that it will
:: be improved by other people. This was designed to work on Windows 7.
:: I have not tested it on any other versions of Windows

if"%1" =="" goto noarg
if"%1" =="/?" goto help
if"%1" =="-?" goto help
if NOT EXIST %1 goto notfound
set taildelay=%2
if"%taildelay%"=="" set taildelay=1

:loop
cls
type %1

:: I use the CHOICE command to create a delay in batch.

CHOICE /C YN /D Y /N /T %taildelay%
goto loop

:: Error handlers

:noarg
echo No arguments given. Try /? for help.
goto die

:notfound
echo The file '%1' could not be found.
goto die

:: Help text

:help
echo TAIL filename [seconds]

:: I use the call more pipe as a way to insert blank lines since echo. doesnt
:: seem to work on Windows 7

call | more
echo Description:
echo     This is a Windows version of the UNIX 'tail' command.
echo     Written completely from scratch by Andrey G.
call | more
echo Parameters:
echo    filename             The name of the file to display
call | more
echo    [seconds]            The number of seconds to delay before reloading the
echo                         file and displaying it again. Default is set to 1
call | more
echo ú  /?                   Displays this help message
call | more
echo    NOTE:
echo    To exit while TAIL is running, press CTRL+C.
call | more
echo Example:
echo    TAIL foo 5
call | more
echo    Will display the contents of the file 'foo',
echo    refreshing every 5 seconds.
call | more

:: This is the end

:die


DOS没有tail命令;您可以在此处下载适用于GNU tail和其他GNU工具的Windows二进制文件。


安装MKS工具包...,以便您可以在Windows上运行所有Unix命令。

该命令是:

1
tail -f <file-name>

如果要使用某些Unix实用程序的Win32端口(而不是安装Cygwin),则建议将GNU实用程序用于Win32。

比Cygwin轻巧,更轻便。


在Far Manager中,按文件上的F3进入标准查看器,然后按End键导航到文件末尾。

如果文件已更新,则Far Manager会自动滚动它。


我正在使用Kiwi Log Viewer。免费。


您也可以尝试WinTail。

??


图形化日志查看器虽然非常适合查看日志文件,但不能满足对可以合并到脚本(或批处理文件)中的命令行实用程序的需求。通常,这种简单通用的命令可用作特定环境的专用解决方案的一部分。图形方法不能轻易地用于这种用途。


我想我已经找到了一个满足批处理文件中tail函数需求的实用程序。它叫做" mtee",它是免费的。我已经将其合并到正在处理的批处理文件中,并且可以很好地完成工作。只需确保将可执行文件放入PATH语句中的目录中,然后就可以走了。

这里是链接:

Mtee