Windows equivalent of the 'tail' command
WindowsCommand LineWindows Problem Overview
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
Windows Solutions
Solution 1 - Windows
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*
Solution 2 - Windows
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.
Solution 3 - Windows
more /e filename.txt P n
where n = the number of rows to display. Works fast and is exactly like head
command.
Solution 4 - Windows
You could get CoreUtils from GnuWin32, which is a collection of standard unix tools, ported to Windows.
It, among other things, contains head.
Solution 5 - Windows
Powershell:
Get-Content C:\logs\result.txt -Tail 10
Get-Content C:\logs\result.txt -wait (monitor the tail)
Solution 6 - Windows
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
^CC:\more test.txt
line 1
line 2
etc......C:
Solution 7 - Windows
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.
Solution 8 - Windows
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.)
Solution 9 - Windows
you can also use Git bash where head and tail are emulated as well
Solution 10 - Windows
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.
Solution 11 - Windows
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)
Solution 12 - Windows
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
Solution 13 - Windows
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.
Solution 14 - Windows
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
Solution 15 - Windows
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.
Solution 16 - Windows
FWIW, for those just needing to snip off an indeterminate number of records from the head of the file, more
Solution 17 - Windows
There's a free head
utility on this page that you can use. I haven't tried it yet.
Solution 18 - Windows
set /p line= < file.csv
echo %line%
it will return first line of your file in cmd Windows in variable %line%.
Solution 19 - Windows
As a contemporary answer, if running Windows 10 you can use the "Linux Subsystem for Windows".
https://docs.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.
Solution 20 - Windows
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
Solution 21 - Windows
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).
Solution 22 - Windows
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.