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
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.
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.
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.
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.
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*
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With