Unix tail equivalent command in Windows Powershell
Unix has a tail command which helps us in reading the file content in the shell window. If the file size is very large then we could run a simple tail command and read the last 100 lines of the file. We do use this in reading the huge logs which will take more time to open or download. So What is the Unix tail equivalent command in Windows Powershell?
PowerShell version 1.o and 2.0 doesn’t have the built-in tail command. However, there is an alternate approach to reading the file contents. Let’s take a look at in detail.
Unix tail equivalent command in PowerShell Version 1.0 and 2.0
Running the below command will show the lines that get added to the file. To see the live example run this command and try to update the text file and come back to the console, you could see the updated text displayed over here.
Get-Content -Path "C:\scripts\test.txt" -Wait
Unix tail equivalent command in PowerShell Version 3.0 and above
PowerShell 3.0 and above comes up with a flag -Tail on Get-Content. The example below demonstrates the -Tail flag.
Get-Content ./log.log -Tail 10
gets the last 10 lines of the file
Get-Content ./log.log -Wait -Tail 10
gets the last 10 lines of the file and waits for more
Note: Get-Content also has aliases defined. For example, if you are used to UNIX you might like cat, and there are also type and gc.
# Print whole file and wait for appended lines and print them
cat <Path> -Wait
# Print last 10 lines and wait for appended lines and print them
cat <Path> -Tail 10 -Wait