Unix Tail Command on Windows to Print Live Logging

Show live logs in windows

Windows is a great operating system for day-to-day use, but there are some things in Linux and Unix that Windows doesn't have out of the box. For example, when I need to SSH into a Linux server, I often want to view log files using the tail command. Unfortunately, Windows doesn't have a native tail command. However, we can create our own function to mimic this simple tail operation using PowerShell.

Using Get-Content
Get-Content -Path logfile.txt -Wait

To mimic the Unix-like tail -f command in PowerShell permanently, you can create a custom function in your PowerShell profile.

1. Open PowerShell or Windows Terminal: First, open Windows PowerShell or Windows Terminal and ensure you're using PowerShell.

2. Modify Your PowerShell Profile: Your PowerShell profile is a script that runs every time you start a new PowerShell session. To edit this profile, open it with any text editor. The profile is typically located at Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1.

3. Create a PowerShell Profile if It Doesn't Exist: If you don't have a profile file yet, you can create one with the following command:
New-Item -Path $PROFILE -ItemType File -Force
4. Add the Tail Function: Now, add the following function to your profile script:
function tail {
    param (
        [Parameter(Position=0, Mandatory=$true)]
        [string]$FilePath,

        [Parameter(Position=1)]
        [int]$n = 10,

        [switch]$f
    )

    if ($f) {
        Get-Content -Path $FilePath -Wait -Tail $n
    } else {
        Get-Content -Path $FilePath -Tail $n
    }
}

5. Reload the Profile: After saving the profile script, reload it to make the function available in your current session:
. $PROFILE

6. Test Your Tail Function: You can now use the tail function similarly to how you would on a Unix-like system. For example:
tail "logs.out" -n 100 -f
Remember, this is just a simple imitation of the tail command, not a full replacement. It mainly creates an alias to the Get-Content command in PowerShell and supports the -f (follow) and -n (number of lines) parameters.

I hope this would be helpfull! As someone who uses Windows OS for day-to-day operations, I find it to be the best operating system for me. I've tried Mac and Ubuntu, but I've never felt as comfortable as I do with Windows. However, I do acknowledge that Windows can be less terminal-friendly compared to Linux because DOS commands are quite different. As a developer, I often use Linux for servers and feel more comfortable with Linux commands, but having these tweaks in Windows helps bridge the gap.

Popular posts from this blog

Spring Kafka - How to use ReplyingKafkaTemplate send and reply synchronously

ERROR 1348 Column Password Is Not Updatable When Updating MySQL Root Password

How To Create Spring Boot Project Using Netbeans