1. Introduction
The cat command, short for concatenate, is one of the most commonly used commands in Linux. It is primarily used to concatenate and display the contents of files. It can also be used to create, modify, or combine files.
The cat command is normally used to view short files or combine files. Basically, it can provide a quick display of the entire file.
When comes to interactive navigation or searching within the file or large file, cat command is not recommended. As its dumps entire file content to the screen at once – not convenient if you want to read through it.
2. Syntax and Options
The cat command takes the following command syntax:
cat options [filename] ...
The following table shows common options for cat:
| Options | Description |
|---|---|
| -b or –number-nonblank | Number the non-blank output lines, starting at 1. |
| -E or –show-ends | Display a dollar sign $ at the end of each line before the newline. |
| -v or –show-nonprinting | Display non-printing characters in caret notation (^) and M- notation, except for the LFD (Line Feed) and TAB characters |
| -e | Equivalent to -vE |
| -T or –show-tabs | Display TAB characters as ^I |
| -A or –show-all | Equivalent to -vET |
| -n or –number | Numbers all output lines starting at 1. |
| -s or –squeeze-blank | Replace multiple adjacent empty lines with a single empty line in the output |
| –help | Display help message |
| –version | Prints version information and exit |
3. Displaying File Contents
To display the content of a file type the cat command followed by the name of file(s).
cat myfile1.txt
This command outputs the content of the file named myfile1.txt to the terminal.
For large files, the only option to navigate the file content is to scroll up using the mouse.
If you wish to view the contents of multiple text files at a go, simply specify the files in one line separated by a space. In this example, we have displayed the contents of two files, one after the other.
cat myfile1.txt myfile2.txt
4. Create a new file
Cat command allows you to create a file and quickly add content to it.