37

Many times I find myself in the situation of having to follow the evolution of a log file on Windows. Is there an equivalent of the Linux

tail -f <filename>

command on a Windows terminal, preferably without having to install external software? Other SO posts talk about installing third-party programs.

  • 2
    Possible duplicate of [Looking for a windows equivalent of the unix tail command](http://stackoverflow.com/questions/187587/looking-for-a-windows-equivalent-of-the-unix-tail-command) – Álvaro González Apr 05 '16 at 15:22
  • EDIT: preferable WITHOUT having to install third-party software –  Apr 05 '16 at 15:24
  • Do writing a bat file or using PowerShell count as installing external software? If they count, then the answer is that it cannot be done. – Álvaro González Apr 05 '16 at 15:24
  • PowerShell yes, .bat file that can be run in a simple cmd is welcome, but again preferably that it is not too much overhead –  Apr 05 '16 at 15:26
  • Microsoft provide a version of `tail` (as part of the resource kit, IIRC) which might be suitable if the restriction is against third-party software specifically rather than all software not shipped as part of Windows. – Harry Johnston Apr 05 '16 at 23:53
  • Bump... Bump... Bump... –  Apr 08 '16 at 13:43

5 Answers5

44

In Powershell you can use Get-Content with the -Wait flag:

Get-Content filename.log -Wait

You can shorten Get-Content to gc. That question suggested as a possible duplicate has an answer which mentions this and some useful extra parameters - see https://stackoverflow.com/a/188126. I'm not sure if it's really a duplicate, though, since that question is talking about general Windows alternatives to Linux tail, rather than about tail -f.

nikobelia
  • 4,595
  • 1
  • 19
  • 27
  • this is not an equivalent since it display whole file to stdout, which is quite useless for multe-megabyte-long log files. `tail -f` does not do this. – andrej Nov 23 '18 at 09:40
  • 9
    This can be used with tail option Get-content filename -Wait -Tail 5. this emulates tail -f behaviour – Venfah Nazir Jan 05 '19 at 05:53
17

In Powershell use:

cat .\<file_name> -Tail 10 -Wait
Xoxole
  • 271
  • 2
  • 6
  • 1
    This one shows the latest lines only, the accepted answer shows whole file from beginning and then newly added lines. This was the way for me. Thanks. – Honza P. Nov 19 '21 at 14:40
6

Yes. you can use tail on windows, which is a small price to pay to get access to a lot of GNU-tools on windows as well as tail. Because its bundle with git for windows, its pretty heavily tested and stable.

First install git-bash from https://gitforwindows.org/

Next, put git-bash on windows path using and reboot your workstation:

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

Now, you should be able to use tail -n 20 -F logging_file.log to tail any file and show the last 20 lines.

If you are on Linux/Unix and you want to continuously see logs you can use the following command: ssh username@10.15.3.3 'bash -c "tail -n 20 -F /c/Users/username/Desktop/logging_file.log"'

alpha_989
  • 4,882
  • 2
  • 37
  • 48
  • Other alternatives: Windows Subsystem for Linux, Cygwin, MinGW. – Mr. Llama Jun 19 '18 at 20:09
  • Yeah.. those are pretty awesome too.. I usually just stick to `git-bash` mainly based on the hypothesis that since `git-bash` comes installed with the default `git` installation for windows, it must be much more tested. `git-bash` actually uses `mingw64`. These solutions are also a bit heavy.. so probably prone to more bugs.. so I try to not complicate things on my remote VMs unless necessary.. Wondering.. did you find any major reason for going to WSL, especially since `Cygwin` has been here much longer? I tried `WSL` about a year back.. and it had bugs all over the place.. – alpha_989 Jun 19 '18 at 20:16
  • 1
    It has full, true, Linux support. It runs actual ELF binaries on an actual Linux kernel. Some packages simply can't be ported to Cygwin or MinGW due to incompatibilities, but they'll work just fine under WSL. – Mr. Llama Jun 20 '18 at 17:57
  • Running `tail -f` in WSL (even in the latest Ubuntu 18.04.1 release) causes my Windows process (an AutoHotkey script) to crash on Exception when the file is appended to. WSL is still buggy. – Joe Coder Aug 01 '18 at 20:26
  • Well, `tail -f`used to work well enough for me until now. I had to reinstall W10 and currently I have the WSL Ubuntu 18.04.3 installed. The command only prints the output when the process, which writes into the log files in windows, is closed and leave the tailed text files. I do not know if something has changed since the previous version – ChesuCR Aug 23 '19 at 18:32
  • Oh, I found a workaround to tail win32 files, check [my answer](https://stackoverflow.com/a/57673519/4891717) if you are interested – ChesuCR Aug 27 '19 at 11:17
6

I know you said without external program. But for the people who have already installed the Windows Subsystem for Linux (WSL) and they cannot make tail work properly in Ubuntu 16.04 LTS I found this thread where somebody found a workaround:

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)

Community
  • 1
  • 1
ChesuCR
  • 9,352
  • 5
  • 51
  • 114
0

Get-Content filename -Wait -tail 1

this worked for me, as said by nikobelia, just added the tail option and it works as expected!