How to tail files in Windows
I’m a big fan of cygwin, so most of the time I have countless small-but-useful utilities at my disposal, without having to think twice. But, when doing system administration on a client’s server in an off-site data center, I’m very limited in what software I’m allowed to put on the machine. For sysadmin tasks, though, unix utilities are pretty hard to do without.
For instance, one of my favorite tools for watching log files is tail, usually with a -f or -F parameter (”follow” and “follow+retry”, respectively; both let you see new lines as the file(s) grow, the latter keeps trying in case the file doesn’t exist yet). Unfortunately, vanilla Windows installations still don’t come with a tail utility. So, I went searching for other ways of tailing files, besides installing cygwin.
One method that I found can be used without any additional software. It’s just a batch file. It can’t follow changes, but you can at least view the last N lines of a file. Here it is, from a FAQ on Microsoft TechNet, by Jerold Schulman:
@echo off
if {%1}=={} @echo FileName parameter required.&goto :EOF
if not exist %1 @echo %1 does NOT exist.&goto :EOF
setlocal
set file=%1
set /a number=10
if not {%2}=={} set /a number=%2
for /f %%i in ('find /v /c "" ^< %file%') do set /a lines=%%i
@echo %lines% lines in file %file%.
if %number% GEQ %lines% set /a start=0&goto console
set /a start=%lines% - %number%
:console
more /e +%start% %file%
endlocal
I really missed tail -F, though, so I went looking again for some way to tail -F without cygwin. There are plenty of freely available utilities out there that should do the trick, but I wanted to find the simplest, safest one, from a trustworthy source, preferrably open-source so the integrity is verifyable. After looking at a couple open-source options, I stumbled across a page on malektips.com: Windows XP and DOS - Unix Style Tail Command
I learned from that page that you can download the Windows Server 2003 Resource Kit Tools package for free, and that it includes a tail utility (including -f functionality). After downloading and installing that on my laptop, I looked in the installation directory, and found tail.exe there. It’s only 7k!
Next, I FTP’d that to the server I needed to run it on, and gave it a try, hoping that it did not have dependencies that required installing the whole Resource Kit Tools package. As it turns out, it worked liked a charm! It’s nice and small, and arguably from a trustworthy source (Microsoft itself). Easier to justify than most of the other tools I was able to find, in case anyone ever questions my putting it on the server.


May 19th, 2007 at 5:56 pm
R:\MEDIA\DATA\READY-TO-DELETE>cat c:\bat\tail.bat
@echo off
IF “%CYGWIN”==”1″ goto :cygwin
:nocygwin
*tail /n%@STRIP[-,%1]
goto :end
:cygwin
c:\cygwin\bin\tail.exe %&
goto :end
:end
May 19th, 2007 at 5:57 pm
I also have a dos tail.exe for non cygwin, but I would only use that if I didn’t have 4NT.
May 21st, 2007 at 10:59 am
Yeah, 4NT kicks ass. Of course, I don’t have it on the aforementioned server… I’m not exactly sure what kinds of restrictions exist in terms of software I can install on this particular server. So, I just try to play it safe.
4NT, cygwin, and sysinternals tools (among other things) go on every other machine that I use on a regular basis, though!