271

I want to display the content of a text file in a CMD window. In addition, I want to see the new lines that added to file, like tail -f command in Unix.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Refael
  • 5,217
  • 9
  • 29
  • 49
  • 5
    http://stackoverflow.com/questions/187587/looking-for-a-windows-equivalent-of-the-unix-tail-command – bryanph Jun 20 '13 at 15:19
  • 1
    If one of these answers has solved your problem, please mark the answer. – Michael Yaeger Sep 10 '16 at 02:29
  • 1
    Possible duplicate of [Looking for a windows equivalent of the unix tail command](https://stackoverflow.com/questions/187587/looking-for-a-windows-equivalent-of-the-unix-tail-command) – Archmede Jun 05 '17 at 19:15

13 Answers13

385

You can use the more command. For example:

more filename.txt

Take a look at GNU utilities for Win32 or download it:

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
limcy_at_mip
  • 3,915
  • 1
  • 13
  • 2
  • How is this similar to tail -f? tail -f loops forever. How did you replicate the -f of it? – Elie Saad Jan 17 '18 at 10:16
  • 6
    This doesnt answer the question. Not sure why its rated so high. OP was looking for 2 things - a windows alternative to cat, and an alternative to tail -f. If you're going to install the GNU utilities for windows, you can then use those things. – speciesUnknown Apr 15 '19 at 14:30
220

We can use the 'type' command to see file contents in cmd.

Example -

type abc.txt

More information can be found HERE.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Anmol Saraf
  • 13,067
  • 9
  • 47
  • 59
  • 1
    I like this answer because it's not interactive. `more` shows N screens at a time that you have to click through – user2023861 Dec 06 '17 at 16:54
26

I don't think there is a built-in function for that

xxxx.txt > con

This opens the files in the default text editor in windows...

type xxxx.txt

This displays the file in the current window. Maybe this has params you can use...

There is a similar question here: CMD.EXE batch script to display last 10 lines from a txt file So there is a "more" command to display a file from the given line, or you can use the GNU Utilities for Win32 what bryanph suggested in his link.

Community
  • 1
  • 1
inf3rno
  • 20,735
  • 9
  • 97
  • 171
8

You can use the 'more' command to see the content of the file:

more filename.txt
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
H.Marroquin
  • 107
  • 1
  • 2
  • 3
    Welcome to StackOverflow and thanks for trying to help. Please explain how the windows version of more can be used to automatically see newly arriving lines in a text file. That would make your answer more directly relate to OPs question, i.e. demonstrate the "tail -f"-compatibility OP wants. – Yunnosch Jun 05 '17 at 19:59
5

Using a single PowerShell command to retrieve the file ending:

powershell -nologo "& "Get-Content -Wait c:\logFile.log -Tail 10"

It applies to PowerShell 3.0 and newer.

Another option is to create a file called TAIL.CMD with this code:

powershell -nologo "& "Get-Content -Wait %1 -Tail %2"
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Eyal
  • 131
  • 1
  • 8
2

To do this, you can use Microsoft's more advanced command-line shell called "Windows PowerShell." It should come standard on the latest versions of Windows, but you can download it from Microsoft if you don't already have it installed.

To get the last five lines in the text file simply read the file using Get-Content, then have Select-Object pick out the last five items/lines for you:

Get-Content c:\scripts\test.txt | Select-Object -last 5

Source: Using the Get-Content Cmdlet

Michael Yaeger
  • 579
  • 9
  • 30
  • @Eyal 's answer is more fitting being called from the cmd, and the code is better using Tail than piping into Select-Object. You can't do -Wait while doing Select Object as well. This is not what the OP is requesting. – Elie Saad Jan 17 '18 at 10:30
2

If you want it to display the content of the file live, and update when the file is altered, just use this script:

@echo off
:start
cls
type myfile.txt
goto start

That will repeat forever until you close the cmd window.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
2

You can do that in some methods:

One is the type command: type filename Another is the more command: more filename With more you can also do that: type filename | more

The last option is using a for for /f "usebackq delims=" %%A in (filename) do (echo.%%A) This will go for each line and display it's content. This is an equivalent of the type command, but it's another method of reading the content.

If you are asking what to use, use the more command as it will make a pause.

Anic17
  • 501
  • 3
  • 12
1

There is no built in option available with Windows. To constantly monitor logs you can use this free application BareTailPro.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Sudheej
  • 1,615
  • 6
  • 21
  • 46
1

You can get the TAIL utility from the Windows Server 2003 Resource Kit Tools.

Here are additional details -- Tail command for Windows (CMD).

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
noni
  • 53
  • 1
  • 7
0

If you want to display for example all .config (or .ini) file name and file content into one doc for user reference (and by this I mean user not knowing shell command i.e. 95% of them), you can try this :

FORFILES /M *myFile.ini /C "cmd /c echo File name : @file >> %temp%\stdout.txt && type @path >> %temp%\stdout.txt && echo. >> %temp%\stdout.txt" | type %temp%\stdout.txt

Explanation :

  • ForFiles : loop on a directory (and child, etc) each file meeting criteria
    • able to return the current file name being process (@file)
    • able to return the full path file being process (@path)
  • Type : Output the file content

Ps : The last pipe command is pointing the %temp% file and output the aggregate content. If you wish to copy/paste in some documentation, just open the stdout.txt file in textpad.

Anic17
  • 501
  • 3
  • 12
0

To show content of a file:

type file.txt - cmd

cat file.txt - bash/powershell

LaurentBaj
  • 65
  • 6
-1
tail -3 d:\text_file.txt

tail -1 d:\text_file.txt

I assume this was added to Windows cmd.exe at some point.

Ian
  • 28,526
  • 18
  • 60
  • 94
noni
  • 7
  • 1
  • 2
    no, `tail` is still not a standard windows command. Usually it's part of an addon like cygwin, GNU or others. – Stephan Jan 29 '16 at 14:20
  • This is not a good answer, you are just saying that this has been implemented into Windows, but no, please check your information before answering. – Anic17 Jun 14 '20 at 16:03