我正在寻找Unix 'tail'命令的等效,这将允许我观察日志文件的输出,而它正在被写入。


当前回答

我写了这个批处理脚本。它不像Unix的“尾巴”那么复杂,但希望有人可以添加它来改进它,比如将输出限制在文件的最后10行,等等。如果你确实改进了这个脚本,请发送给我在抢劫~[at]~ gmail.com。

@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

其他回答

有相当多的选择,但他们都有更高级的功能缺陷。

GnuWin32尾巴是错误的(α β γ) -像-f只是不工作。 UnxUtils尾部似乎更好(-f工作,但-pid似乎不是,-n但不是——lines=n失败与-f),但似乎是一个死亡的项目。 Cygwin是一个巨大的丑陋的混乱,可能只需要使用DLL和coreutils包-但仍然存在问题,如pid不能与本机win32进程一起工作。

如果你根本不想安装任何东西,你可以用标准的Windows命令“构建你自己的”批处理文件。这里有一些关于如何做到这一点的建议。

1)使用find /c /v "" yourinput。文件,获取输入文件中的行数。输出如下所示:

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

2)使用for /f,解析此输出以得到数字15。

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

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

如果我有时间,我将构建这样一个批处理文件,并将其发布回这里。

编辑:tail.bat

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

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

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

它不是完美的,有时台词会重复。

用法:tail.bat -d 尾巴,蝙蝠-f -f

@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

下载tail命令,这是微软自己提供的Windows Server 2003资源工具包工具的一部分。