The tail command is the Unix operating system command-line utility and can only run-on Unix. This command is used for reading files from the tail or bottom to monitor the file from the bottom. On the Windows platform, we can use it by utilizing PowerShell 3.0 and later, which adds the “tail” flag.
In this article, we will demonstrate various examples of Windows equivalent of Unix tail commands.
Windows Equivalent of Unix tail Command?
The tail command is used to read files from the button. The latest version of Windows PowerShell introduces the “tail” flag that can be used with the “Get-Content” command to access file content from a button like Unix “tail” command.
Here are some listed examples that will elaborate on Windows commands that are Unix tail versions.
Example 1: Using “Get-Content” With “Tail” Flag
Execute the provided command and specify the file name from where you want to access file content. Use the tail option and specify the value for the number of lines you want to read from the bottom of the file:
In the above command, the “Get-Content” is used to get the content of a specified file, and at the end “-Tail 1” is used to read the last line of the file or one line of the file from the bottom.
Let us say we want to get the last three lines of “file3.txt”, we will just modify the “Tail” option value:
Here you can see that we have successfully read the last three lines of file “file3.txt”. The Wait option is used for waiting until user press any key to terminate command:
Let us try to get the last two lines of “file3.txt” without using “-Tail” option:
You can see that we have access two lines of files from the button:
Example 2: Using Get-Content With “Select-Object” Cmdlet
If your system PowerShell not supporting “Tail” option, then we can use the alternative command “Select-Object” with specific property “last”:
Syntax
Whereas the command “Get-Content” is used to obtain file’s content, the command “Select-Object” will choose an object based on a defined property, and “n” is the maximum number of objects or lines the user can request:
Let us access “file3.txt” using “Get-Content” command and specify the value of “n” to read or get numbers of object or line:
You can see that we have successfully read the last three lines of the “file3.txt” file.
We have compiled the Windows PowerShell commands that are equivalent to Unix tail commands.
Conclusion
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. Moreover, you can use the “Get-Content” with the “Select-Object” cmdlet to make it equivalent to the tail command in Linux. In this article, we have given examples of PowerShell commands that are Windows versions of Unix tail commands.