128

Is there a way to simulate the *nix tail command on the Windows command line? I have a file and I want a way to snip off the first n lines of text. For example:

D:\>type file.txt
line one
line two
line three
D:\>*[call to tail]* > result.txt

D:\>type result.txt
line two
line three
Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
Chris Smith
  • 18,244
  • 13
  • 59
  • 81
  • 12
    Doesn't `head` show only the first *n* lines instead of leaving them out? – Joey Aug 19 '09 at 01:30
  • Please Chris consider the new answers since 2009, more specifically the [Amit Portnoy's answer](http://stackoverflow.com/a/14341672/938111). As your question is general, many user may find this web page. And they may stop reading the answers after the first one: the answer you have accepted. You can change the answer you have accepted to another more updated to nowadays possibilities. Cheers ;) – oHo Sep 24 '13 at 08:34
  • I'm a tad confused. The original question was about the Unix style `head` command, but the desired output looked like it was wanting `tail`. It looks like the answers are about `head` and not `tail`. – blakeoft Mar 04 '15 at 21:54
  • @blakeoft the original question was asking for the behaviour of `tail`, however the title said `head` – M.M Mar 04 '15 at 22:32
  • It is not clear in the example if the intended command is `*[call to tail]* 2 > result.txt`, in which case it is equivalent to `tail`, or `*[call to tail]* 1 > result.txt`, in which case it is not. – sancho.s ReinstateMonicaCellio Apr 09 '16 at 10:35

22 Answers22

132

IF you have Windows PowerShell installed (I think it's included since XP) you can just run from cmd.exe:

Head Command:

powershell -command "& {Get-Content *filename* -TotalCount *n*}"

Tail Command:

powershell -command "& {Get-Content *filename* | Select-Object -last *n*}"

or, directly from PowerShell:

Get-Content *filename* -TotalCount *n*
Get-Content *filename* | Select-Object -last *n*


update

PowerShell 3.0 (Windows 8 and higher) added Tail command with alias Last. Head and First aliases to TotalCount were also added.

So, commands can be re-written as

Get-Content *filename* -Head *n*
Get-Content *filename* -Tail *n*
Amit Portnoy
  • 5,957
  • 2
  • 29
  • 30
  • Doesn't this behave like `head` instead of `tail`? – blakeoft Mar 04 '15 at 21:51
  • @blakeoft yes it did... I've updated my answer. It's amazing that it took 2 year and 17 votes up before someone commented on this. – Amit Portnoy Mar 04 '15 at 22:12
  • 1
    @blakeoft just realized that the question was edited! (see stackoverflow.com/posts/1295068/revisions). It originally asked for the head command... updated to include both answers... – Amit Portnoy Mar 05 '15 at 09:31
  • 2
    @AmitPortnoy I found another answer somewhere on this stack exchange saying that `Get-Content` has a tail command now. For example, `Get-content -Tail 5 file.txt` will print the last five lines of file.txt. Thanks for updating your answer btw. – blakeoft Mar 05 '15 at 13:59
  • 2
    any way to use it as a filter? (eg. reading stdin instead of file) – eadmaster Apr 22 '16 at 03:27
  • actually it's included since Vista – phuclv Jan 12 '18 at 10:15
  • PowerShell cannot be run in any DOS system I know. – Andreas Rejbrand Jan 12 '18 at 10:26
102

No exact equivalent. However there exist a native DOS command "more" that has a +n option that will start outputting the file after the nth line:

DOS Prompt:

C:\>more +2 myfile.txt

The above command will output everything after the first 2 lines.
This is actually the inverse of Unix head:

Unix console:

root@server:~$ head -2 myfile.txt

The above command will print only the first 2 lines of the file.

Matthew Nizol
  • 2,609
  • 1
  • 18
  • 22
  • 12
    No, it's not the same. `tail` starts counting from the end of file. So, `tail -2` will print the last two lines. – ADTC Nov 20 '13 at 11:16
  • 7
    Can I print first n characters too? – Qwerty Jan 20 '14 at 23:19
  • @ADTC (my version of) tail doesnt support negatives. only head does (gnuwin32) – nl-x May 17 '16 at 08:02
  • @nl-x `-2` is not a negative. It's just a shorthand version of `-n 2` (number of lines). Unfortunately, far too many implementations of `tail` do not support `-2`, making it very inconsistent with accompanying `head` implementations which do. It has annoyed me one too many times. – ADTC May 17 '16 at 15:21
  • @ADTC Ah sorry. I was confused with `head -n -2` which is different from `head -n 2`. But with tail, the latter doesn't exist. – nl-x May 17 '16 at 15:30
  • Be careful if using this with redirection as implied in the question. Redirected output pauses after 65535 lines. – Andy Harfoot Mar 09 '18 at 10:51
  • @ADTC most versions of `tail` also support a "how many lines from the top" behavior by prefixing the number with `+`. So regarding the example above, to get everything after the second line (e.g. the third line and below), use `tail -n +3` – Ken Bellows Jul 20 '21 at 10:47
25
more /e filename.txt P n

where n = the number of rows to display. Works fast and is exactly like head command.

phuclv
  • 37,963
  • 15
  • 156
  • 475
Dan
  • 347
  • 3
  • 2
  • 18
    Commands such as P, S, etc. can only be input by the user at the prompt. They cannot be passed-in to the more command. Thus, the example above does not work – Philibert Perusse Jul 23 '12 at 22:16
23

Powershell:

Get-Content C:\logs\result.txt -Tail 10

Get-Content C:\logs\result.txt -wait (monitor the tail)
Manuel Alves
  • 3,885
  • 2
  • 30
  • 24
  • 4
    This is the correct answer, up to date and relevant (unlike cmd). Thanks. – guychouk Aug 15 '17 at 08:48
  • 1
    You can also combine them: Get-Content c:\Logs\Results.txt -Tail 10 -wait This will show the last 10 lines and wait for new entries. – Semperfi89 Mar 16 '23 at 19:28
18

You could get CoreUtils from GnuWin32, which is a collection of standard unix tools, ported to Windows.

It, among other things, contains head.

Sebastian Paaske Tørholm
  • 49,493
  • 11
  • 100
  • 118
13

This is a total hack but if it's a huge file that you want to just examine the format, header, etc. and you're looking for a solution you can always just redirect the 'more' output to a new file and CTRL-C quickly. The output rows can't be controlled precisely and you will most likely kill it in the middle of a line of output but it's a cheap way of grabbing a small bit of an otherwise unusable file.

Ex.

C:\more test.csv > test.txt 
^C C:\more test.txt
line 1
line 2
etc...... C:\
Walfie
  • 3,376
  • 4
  • 32
  • 38
user2041301
  • 139
  • 1
  • 2
9

Well, this will do it, but it's about as fast as it looks (roughly O(n*m), where n is the number of lines to display and m is the total number of lines in the file):

for /l %l in (1,1,10) do @for /f "tokens=1,2* delims=:" %a in ('findstr /n /r "^" filename ^| findstr /r "^%l:"') do @echo %b

Where "10" is the number of lines you want to print, and "filename" is the name of the file.

brianary
  • 8,996
  • 2
  • 35
  • 29
  • 6
    +1 for a solution that took you longer to type than downloading a port of `head`. – Camilo Martin Apr 10 '12 at 22:35
  • 1
    Note: Put this expression in brackets - `(expr) > out.txt` - when sending the results to a file, or you'll only get the last line. Or append - `expr >> out.txt`. – c z Jun 02 '20 at 08:29
8

When using more +n that Matt already mentioned, to avoid pauses in long files, try this:

more +1 myfile.txt > con

When you redirect the output from more, it doesn't pause - and here you redirect to the console. You can similarly redirect to some other file like this w/o the pauses of more if that's your desired end result. Use > to redirect to file and overwrite it if it already exists, or >> to append to an existing file. (Can use either to redirect to con.)

Anon
  • 11,870
  • 3
  • 23
  • 19
6

you can also use Git bash where head and tail are emulated as well

brigasnuncamais
  • 127
  • 2
  • 4
  • 1
    Also the official Bash on Windows https://msdn.microsoft.com/en-us/commandline/wsl/install_guide?f=255&MSPPError=-2147217396 – the_nuts Jan 13 '17 at 14:51
5

Get-content -Tail n file.txt with powershell is the only thing that comes close to tail in linux.

The Get-Content *filename* | Select-Object -last *n* suggested above loads/parse the whole thing. Needless to say, it was not happy with my 10GB log file... The -Tail option does start by the end of the file.

Sebas
  • 21,192
  • 9
  • 55
  • 109
  • I'm not sure if it does start by the end but -Tail took several minutes for a `-Tail 100` until I cancelled it. The file was slightly below 800MB – Noman_1 Jul 18 '22 at 11:15
3

in PS try to use command:

Select -Last 1

This command can be pipelined also.

Example to get first line:

type .\out.txt | Select -Last 1

or to get the first line:

 type .\out.txt | Select -First 1
Gico
  • 1,276
  • 2
  • 15
  • 30
2

If you want the head command, one easy way to get it is to install Cygwin. Then you'll have all the UNIX tools at your disposal.

If that isn't a good solution, then you can try using findstr and do a search for the end-of-line indicator.

findstr on MSDN: http://technet.microsoft.com/en-us/library/bb490907.aspx

Zian Choy
  • 2,846
  • 6
  • 33
  • 64
2

Here is a fast native head command that gives you the first 9 lines in DOS.

findstr /n "." myfile.txt | findstr "^.:"

The first 2 characters on each line will be the line number.

sashkello
  • 17,306
  • 24
  • 81
  • 109
Don4x
  • 21
  • 1
2

There is a resource kit that can be downloaded from here: http://www.microsoft.com/downloads/en/confirmation.aspx?familyId=9d467a69-57ff-4ae7-96ee-b18c4790cffd&displayLang=en

It contains a tail.exe tool but it is only compatible with some Windows versions

(Copied from this post: Tail command for windows)

Sriniv
  • 131
  • 1
  • 5
rasputino
  • 691
  • 1
  • 8
  • 24
1

I have not tried extracting a range, but I was able to get a line using the following DOS command:

find /N " " *.log|find "[6]" 

Since most files contain spaces, this command pulls every line from all LOG files and basically numbers them starting from 1 for each file. The numbered results are then piped into the second FIND command which looks for the line tagged as number 6.

GDP
  • 8,109
  • 6
  • 45
  • 82
1

FWIW, for those just needing to snip off an indeterminate number of records from the head of the file, more > works well. This is useful just to have a smaller file to work with in the early stages of developing something.

user382459
  • 141
  • 2
  • 3
0

There's a free head utility on this page that you can use. I haven't tried it yet.

Gerhard
  • 22,678
  • 7
  • 27
  • 43
JeffH
  • 10,254
  • 2
  • 27
  • 48
0
set /p line= < file.csv 
echo %line%

it will return first line of your file in cmd Windows in variable %line%.

Sergey Orlov
  • 349
  • 2
  • 5
0

As a contemporary answer, if running Windows 10 you can use the "Linux Subsystem for Windows".

https://learn.microsoft.com/en-us/windows/wsl/install-win10

This will allow you to run native linux commands from within windows and thus run tail exactly how you would in linux.

Alex KeySmith
  • 16,657
  • 11
  • 74
  • 152
0

To keep the 1st few lines of text (head):

set n=<lines>
for /l %a in (1,1,%n%) do (
for /f "tokens=*" %i in ('find /v /n "" ^< test1.txt ^| find "[%a]"') do (
REM ove prefixed line numbers:
set a=%i
set a=!a:*]=! 
echo:!a!)
)

To discard the 1st few lines of text (tail):

set n=<lines>
set file=<file.txt>
set /a n=n+1 >nul
for /f "tokens=*" %i in ('find /v /c "" ^< %file%') do set total=%i
for /l %a in (%n%,1,%total%) do (
for /f "tokens=*" %i in ('find /v /n "" ^< %file% ^| find "[%a]"') do (
REM ove prefixed line numbers:
set a=%i
set a=!a:*]=! 
echo:!a!)
)

Note:
The head function can also be achieved by fsutil
The tail function can also be achieved by fc & comp

Tested on Win 10 cmd

Zimba
  • 2,854
  • 18
  • 26
-2

I don't think there is way out of the box. There is no such command in DOS and batch files are far to limited to simulate it (without major pain).

EricSchaefer
  • 25,272
  • 21
  • 67
  • 103
-2

Warning, using the batch file for, tokens, and delims capability on unknown text input can be a disaster due to the special interpretation of chars like &, !, <, etc. Such methods should be reserved for only predictable text.