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

  1. View file contents
  2. Concatenate files
  3. Create new files
  4. Display line numbers
  5. 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

  1. Create file with input:

    cat > newfile
  2. Append to file:

    cat >> existing_file
  3. 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

  1. Use less for large files
  2. Avoid cat for binary files
  3. Consider line ending issues
  4. Use appropriate options for visibility
  5. Be careful with redirection