Printing the last or first, few lines of a file is a common operation in day to day operations.
On Linux most people will, without thinking twice, use tail
and it's counterpart head
to achieve this.
It's man page1 describes tail simply like this
Print the last 10 lines of each FILE to standard output.
With more than one FILE, precede each with a header giving the file name.
Lets take a look at some common tail operations and how to reproduce them with PowerShell.
tail -f
tail -f /var/log/important.log
This is my personal top use case for tail. The -f
switch instructs tail to follow the given file,
outputting data as it is written to the file.
With PowerShell we will use Get-Content
and instruct it to follow the file with the -Wait
switch.
Get-Content "c:\important.log" -Wait
Important!
There is one important difference to tail
.
If the file being followed gets deleted the process will return with a non-zero code.
Terminating is the same with both tail
and Get-Content
, just press Ctrl+C
tail -n
The -n
parameter instructs tail to return the last X lines from the given file.
Let's say you want the last 20 lines from your important log file
tail -n 20 /var/log/important.log
PowerShell can replicate this behavior with the -Tail
parameter.
Get-Content "c:\important.log" -Tail 20
head -n
Head works the same way, but takes the first X lines from the given file.
head -n 20 /var/log/important.log
PowerShell can replicate this behavior with the -Head
parameter.
Get-Content "c:\important.log" -Head 20
Multiple Files
Coming back to our tail -f
example you may want to operate on multiple files at once.
With tail you can simply append them to the end of the command like this.
tail -f /var/log/important.log /var/log/another.log
Not surprisingly Get-Content
works in a similar way.
Get-Content "c:\important.log","c:\another.log" -Wait
Important!
tail
the different files need to be comma-separated
Shortcuts
Writing out Get-Content
every time may seem a bit long winded or cumbersome, thankfully you don't have to.
With some shortcuts you can significantly lower keystrokes:
- Shorten
Get-Content
by using it's aliasgc
- Omit
""
around your paths as long as they have no spaces inside - Commands are always case insensitive, this is more of a personal preference
With this shortcuts our commands are way shorter.
Get-Content "c:\important.log" -Wait
gc c:\important.log -Wait
Get-Content "c:\important.log" -Tail 20
gc c:\important.log -Tail 20
Get-Content "c:\important.log" -Head 20
gc c:\important.log -Head 20
Get-Content "c:\important.log","c:\another.log" -Wait
gc c:\important.log,c:\another.log -Wait
Taking It Further
This covers examples for basic tail usage to get you started in PowerShell.
Additionally you can use a wide variety of wildcards inside the path to filter for multiple files to tail.
Get-Content "c:\logs\*" -Wait
Advancing from there you may want to exclude some irrelevant IIS logs and a few old log files from 2019.
Get-Content "c:\logs\*" -Exclude "iis-*.log","*2019*" -Wait
A great ressource to dive in even deeper is the official documentation from microsoft.
Feel free to ask me about your PowerShell problem over on Twitter!
Top comments (0)