Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows equivalent of the 'tail' command [duplicate]

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 
like image 754
Chris Smith Avatar asked Aug 18 '09 16:08

Chris Smith


People also ask

Is there an equivalent of tail in Windows?

Traditionally tail has been used to view the bottom X number of lines from a log file. While Windows doesn't have a standalone utility to do what tail does, we do have the Get-Content PowerShell cmdlet which happens to have a tail parameter.

Does PowerShell have a tail command?

The Tail command is popular in the Unix language and it is used to retrieve the specific number of lines from the end of the document or the log files. PowerShell doesn't have the command with the same name but from the PowerShell v3. 0 onwards, PowerShell has added -Tail parameter in the Get-Content cmdlet.

How do you repeat a command in Windows?

While at the MS-DOS prompt or in the Windows command line you can quickly repeat any previously entered command and view a history of commands using the arrow keys. For example, if you previously used the dir command to list the files in the current directory press the up arrow key to repeat that command.

How do you tail a file in Windows PowerShell?

Open it with notepad $PROFILE. Then in the text document, create a new function: function Tail ($path) { Get-content -tail 15 -path $path -wait } This way you can access the function each time you start PowerShell. This should be the accepted answer.


1 Answers

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* 
like image 186
Amit Portnoy Avatar answered Sep 19 '22 16:09

Amit Portnoy