The cat command (short for “concatenate”) is one of the most fundamental Linux utilities. It reads files and displays their content to standard output, making it essential for viewing file contents, combining files, and creating simple text files.
Key Concepts
- Concatenation: Joining multiple files together
- Standard Output: Default destination for command output (usually terminal)
- Standard Input: Source of input data (keyboard by default)
- Redirection: Sending output to files or other commands
- Here Document: Inline input method using
<<
Command Syntax
cat [options] [file...]
- Reads from files or standard input
- Outputs to standard output by default
- Multiple files are concatenated in order
Common Options
-n - Number all output lines
-b - Number only non-empty lines
-s - Suppress multiple blank lines
-A - Show all characters (including hidden)
-E - Show line endings with $
-T - Show tabs as ^I
Practical Examples
Example 1: Display file contents
|
|
Shows the contents of the password file
Example 2: Concatenate multiple files
|
|
Displays contents of all three files in sequence
Example 3: Create a new file
|
|
Creates a file with the typed content
Example 4: Append to existing file
|
|
Adds content to the end of existing file
Example 5: Number lines
|
|
Shows file with line numbers
Example 6: Using here document
|
|
Creates file with specified content
Use Cases
- Quick file viewing: Check configuration files
- Log file examination: View recent log entries
- File combination: Merge multiple text files
- Simple file creation: Create small text files
- Piping data: Send file contents to other commands
- Script debugging: Display variable contents
Related Commands
less - View files page by page
more - Simple pager for file viewing
head - Show first lines of file
tail - Show last lines of file
tac - Display file in reverse order
nl - Number lines (alternative to cat -n)
Tips & Troubleshooting
Performance Considerations
- Avoid
caton very large files (uselessinstead) - For binary files, use
hexdumporod - Use
headortailfor partial file viewing
Common Issues
- Binary file display: May corrupt terminal display
- Solution:
resetcommand to fix terminal
- Solution:
- Permission denied: Check file permissions
- Solution:
ls -l filenameto verify access
- Solution:
- File not found: Verify file path and existence
- Solution: Use
lsorfindto locate file
- Solution: Use
Security Notes
- Be cautious with
caton unknown files - Some files may contain control characters
- Always verify file source before displaying
Useful Combinations
|
|
Memory Efficiency
catloads entire file into memory- For large files, consider streaming tools
- Use
splitfor processing huge files in chunks