Linux cat
Command – Concatenate and Display File Content
The cat
(short for "concatenate") command is one of the most commonly used commands in Linux for viewing, creating, and combining text files. It is a simple yet powerful tool to display the content of files and is especially useful for quick file viewing, combining multiple files, or redirecting output.
Syntax of cat
OPTIONS
→ Flags that modify the behavior ofcat
.FILE(s)
→ The name(s) of the file(s) you want to view or manipulate.
Common Usage of cat
1. Display the Content of a File
To display the contents of a file on the terminal:
Example:
This will display the content of document.txt in the terminal window.
2. Display Multiple Files
To display the contents of multiple files one after another:
Example:
This will show the content of file1.txt followed by file2.txt.
3. Create a New File
You can create a new file and add content to it using cat
:
This will allow you to type text. Once you're done, press Ctrl + D
to save the file and exit.
Example:
This creates notes.txt with the text "This is a note."
4. Append Content to an Existing File
To append content to an existing file, use the >>
operator:
Example:
This appends "This is additional content." to notes.txt.
5. Concatenate and Display Files Together
To combine multiple files and display their content in a single output:
This will concatenate the contents of file1.txt and file2.txt into a new file named combined.txt.
6. Number Lines of a File
To display the contents of a file with line numbers, use the -n
option:
Example:
This will display document.txt with line numbers before each line.
7. Show Line Numbers Only for Non-Empty Lines (-b
)
If you want to display line numbers only for non-empty lines, use the -b
option:
Example:
This will number only the non-blank lines of document.txt.
8. Show Non-Printable Characters (-v
)
If you want to display non-printable characters (like tabs and newlines) in the output, use the -v
option:
Example:
This will display document.txt and any special or non-printable characters will be visible.
9. Suppress Repeated Empty Lines (-s
)
To suppress consecutive empty lines and display only a single empty line between sections, use the -s
option:
Example:
This will remove any repeated blank lines in document.txt, leaving only one blank line between paragraphs.
Examples
1. Display a Single File
This will display the content of myfile.txt in the terminal.
2. Concatenate Multiple Files
This will display the contents of file1.txt followed by the contents of file2.txt.
3. Create and Write to a File
This will create newfile.txt and write the text "Hello, this is a new file." inside.
4. Append Content to a File
This appends the text "This is the appended content." to existingfile.txt.
5. Show Line Numbers
This will display myfile.txt with line numbers before each line.
6. Concatenate Files into One
This will combine the content of file1.txt and file2.txt into a new file called combined.txt.
7. View Non-Printable Characters
This will display myfile.txt, showing any special characters like tabs or newlines.
Error Handling with cat
File Not Found: If the specified file does not exist, you will get an error like:
Permission Denied: If you don't have permission to read the file, you'll see an error:
Conclusion
The cat
command is one of the most versatile and commonly used commands in Linux for working with text files. It allows you to display, create, append, and concatenate files with ease. Whether you need to view file contents, combine files, or add content, cat
is an essential tool for managing text files in Linux.
Would you like additional details or SEO optimization? 🚀