cat
Overview
The cat
(concatenate) command reads files and prints their contents to standard output. It can also concatenate multiple files and create new ones.
Syntax
cat [options] [file...]
Common Options
Option | Description |
---|---|
-n |
Number all output lines |
-b |
Number non-blank output lines |
-s |
Suppress repeated empty lines |
-v |
Show non-printing characters |
-E |
Show line endings ($) |
-T |
Show tabs (^I) |
-A |
Show all non-printing characters |
--help |
Display help message |
--version |
Output version information |
Key Use Cases
- View file contents
- Concatenate files
- Create new files
- Display line numbers
- Show special characters
Examples with Explanations
Example 1: View File
cat file.txt
Display contents of file.txt
Example 2: Concatenate Files
cat file1 file2 > combined
Combine file1 and file2 into new file
Example 3: Number Lines
cat -n file.txt
Show file contents with line numbers
Understanding Output
- Raw file contents
- With -n:
- Line numbers followed by content
- With -A:
- Special characters visible
- Error messages for:
- File not found
- Permission denied
- Binary file notice
Common Usage Patterns
Create file with input:
cat > newfile
Append to file:
cat >> existing_file
Show non-printing chars:
cat -A file
Performance Analysis
- Best for small files
- Memory usage considerations
- Terminal output limitations
- Line buffering impact
- Multiple file handling
Additional Resources
Best Practices
- Use less for large files
- Avoid cat for binary files
- Consider line ending issues
- Use appropriate options for visibility
- Be careful with redirection