Concatenate, or CAT, is a command that can be used to create single or multiple files, read a file’s contents, concatenate multiple files, copy the content to other files, and print output to a file or the terminal.
Most Linux and other operating systems use the cat command. As it concatenates files, the cat command is known as CAT. Use the cat command to read the contents of a file, or the cat filename.txt command to view a huge file.
Use The Cat Command In Windows PowerShell With Syntax (Concatenate Files)
- The PowerShell Cat Command
- Using PowerShell Cat to Show a File’s Contents
- Displaying Lines from the Start
- Displaying Line from the Bottom
- Creating a New File by Merging Contents
- Addition of a File’s Contents to Another
- File Filtering
1. The PowerShell Cat Command
Cat command used to read a file’s contents that some Linux users may be familiar with. The cat command in PowerShell serves the same purpose. But take note that Get-Content is the Get-Content cmdlet’s real name, not the PowerShell cat.
You may find out if the Get-Content command has any other aliases besides cat by performing the command below.
Get-Alias -Definition Get-Content
Get-Command also has the synonyms Get-Content GC and type.
2. Using PowerShell Cat to Show a File’s Contents
The PowerShell cat’s primary and most fundamental use is to display the contents of a file on the screen. The cat command, when used after the filename, instructs the command to just output the contents of the file for display.
Run the command listed below to read the File.txt file and display the data.
The contents of File.txt ought to appear on your screen.
3. Displaying Lines from the Start
It’s not always practical to read or show a file’s whole contents, especially when working with large files that have hundreds or more lines.
It may be possible to tell if a file is what you need by reading the first few lines of the file. You can quickly check a line or lines from a file by displaying them using PowerShell CAT Command.
By specifying the -TotalCount argument after the desired number of lines, you can display a selection of lines starting at the file’s beginning. You can interchangeably use the aliases -Head and -First for the -TotalCount argument.
Run the command listed below to see the first five lines of file.txt.
The aliases -Head and -First for the -TotalCount option are also interchangeable.
file.txt cat -TotalCount 5
Only the first five lines of the output will be shown.
4. Displaying Line from the Bottom
By giving the -Tail argument or its equivalent, which is -Last, you can also examine the file’s contents from the bottom up. This approach is typical for log file debugging, as it is normal to just be aware of the most recent batch of entries prior to a program crash.
View the final five lines of File.txt by running cat with the -Tail option.
cat File.txt -Tail 5
The final five lines from the bottom should appear in your terminal’s output.
5. Creating a New File by Merging Contents
In PowerShell, you may send the standard output of a command to a new file rather than just displaying the material on the screen. It is possible to merge the contents of files because the PowerShell cat may read many files at once.
To combine File.txt and File.txt, use the cat command as shown below. The output redirect (>) command transfers the output to a new file with the name text.txt.
cat File.txt,File.txt > Merge.txt
Instead of employing the redirect operator (>), the same outcome can also be obtained by pipelining the output from cat to Out-File.
cat File.txt,File.txt | Out-File Merge.txt
To see the following information in the new Merge.txt file, run the CAT once more.
6. Addition of a File’s Contents to Another
The two PowerShell methods for appending material to a file are listed below. The Add-Content cmdlet receives the PowerShell cat output (|) using the first approach. The double output redirection sign (>>) is used in the second technique.
Run any of the commands listed below, and the outcome will be the same.
# PowerShell cat with Add-Content
cat File.txt | Add-Content File.txt
# PowerShell cat with redirection symbol (append)
cat File.txt >> File.txt
To verify the File2.txt file’s newly added content, issue the following command.
7. File Filtering
Wildcards and patterns are both acceptable location inputs for the PowerShell cat. When you need to filter, include, or exclude things from a list of files with mixed filenames or kinds, this functionality is helpful.
All Files Are Displayed
There are three files in your current working directory: two *.txt files and one *.log file. Use the asterisk (*) wildcard at the end of the path to show the contents of all files in this directory.
The contents of all files should be instantly shown on your terminal.
Files with Specific File Extensions are Displayed
There are numerous methods to go about it. You only need to read *.txt files, for instance. Applying the wildcard to the input path is the first method. Read all files with the *.txt extension.
Another option is to use the wildcard pattern followed by the -Include parameter. Use the command shown below to read all of the folder’s files, but only those with the *.txt extension.
cat .\\* -Include *.txt
Add the -Exclude argument in place of selecting which files to include. Run the code below to exclude File2.txt from the list of files with the *.txt extension.
cat .\\* -Filter *.txt -Exclude File2.txt
The output is paginated
When you use the cat to view huge files, a quick stream of text appears that continues till the bottom. Reading the contents of a file is not made all that easy by this behavior.
You can use pagination in your output to manageably read enormous files on the screen. You can only see the contents of a page using pagination if they fit in the window.
Bypassing the PowerShell cat output through the built-in more function or the Out-Host cmdlet, pagination can be accomplished.
Run one of the instructions below to paginate the File.log. Any command will result in the same outcome and interaction possibilities.
cat File.log | more
cat File.log | Out-Host -Paging
Only the contents that fit your current window size are displayed in the result.
There are now three ways that you can interact.
- To see the entire next page, press SPACE.
- To display the next line, hit Enter or carriage return (CR).
- To stop the pagination, press Q.