Windows equivalent of the 'tail' command

Viewed 275766

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
22 Answers

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

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

Related