Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell tail multiple files command

Tags:

powershell

I can tail one file via the following command:

Get-Content -Path C:\log1.txt -Tail 10 –Wait

How do I extend this to multiple files, I have tried the following with no luck:

Get-Content -Path C:\log1.txt,C:\log2.txt -Tail 10 –Wait

This will only pick up updates from the first file, not the second.

like image 288
Cheetah Avatar asked Feb 17 '15 17:02

Cheetah


People also ask

Can you tail multiple files?

To tail multiple files with multitail simply invoke the program followed by the two filenames you wish to follow. The nice thing about multitail is it creates two panes, one for each file. These panes can scroll independently and search works across both panes.

Is there a tail command in PowerShell?

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.

Can you grep in PowerShell?

When you need to search through a string or log files in Linux we can use the grep command. For PowerShell, we can use the grep equivalent Select-String . We can get pretty much the same results with this powerful cmdlet. Select-String uses just like grep regular expression to find text patterns in files and strings.

Is there a tail command for Windows?

Tail command is a UNIX command line utility used to read file content from the bottom of the file. The Latest version of Windows PowerShell introduces the “Tail” as an option. The Windows PowerShell command “Get-Content” with the option “Tail” is equivalent to the Unix tail command.


2 Answers

Based on @mjolinor's comment, I have come up with the following that appears to work,

Workflow My-Tail
{
    Param([string[]] $Path)

    foreach -parallel ($file in $path)
    {
        Get-Content -Path $file -Tail 1 -Wait
    }
}

My-Tail (dir C:\*.log -Include log1.txt,log2.txt)

However, this has some sort of progress bar that appears...

like image 195
Cheetah Avatar answered Nov 03 '22 01:11

Cheetah


I can't speak to how efficient this is, but since I'm using PowerShell Core 7.1.3, I can't use Workflows or ForEach -Parallel, but I can use ForEach-Object -Parallel, so I tried it just to see what would happen...

gci -Path C:\ -Filter log*.txt |
    % -Parallel {
        cat -Wait -Tail 10 -Path $_
    } -ThrottleLimit 30

In my case, I had 27 files I needed to monitor, so I chose a number just above that, and this seemed to work.

Just to be sure it was working, I used this, which will output the source file name before each line:

gci -Path C:\ -Filter log*.txt |
    % -Parallel {
        $file = $_;
        cat -Wait -Tail 10 -Path $file |
            % { write "$($file.Name): ${_}" }
    } -ThrottleLimit 30
like image 37
Chad Baldwin Avatar answered Nov 02 '22 23:11

Chad Baldwin