cat

Comprehensive guide to the cat command for viewing, creating, and manipulating text files in Linux

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

1
cat /etc/passwd

Shows the contents of the password file

Example 2: Concatenate multiple files

1
cat file1.txt file2.txt file3.txt

Displays contents of all three files in sequence

Example 3: Create a new file

1
2
3
cat > newfile.txt
Type your content here
Press Ctrl+D to save and exit

Creates a file with the typed content

Example 4: Append to existing file

1
2
3
cat >> existing.txt
Additional content here
Ctrl+D to finish

Adds content to the end of existing file

Example 5: Number lines

1
2
3
4
5
cat -n script.sh
     1  #!/bin/bash
     2  echo "Hello World"
     3  
     4  exit 0

Shows file with line numbers

Example 6: Using here document

1
2
3
4
5
cat << EOF > config.txt
server=localhost
port=8080
debug=true
EOF

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

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 cat on very large files (use less instead)
  • For binary files, use hexdump or od
  • Use head or tail for partial file viewing

Common Issues

  • Binary file display: May corrupt terminal display
    • Solution: reset command to fix terminal
  • Permission denied: Check file permissions
    • Solution: ls -l filename to verify access
  • File not found: Verify file path and existence
    • Solution: Use ls or find to locate file

Security Notes

  • Be cautious with cat on unknown files
  • Some files may contain control characters
  • Always verify file source before displaying

Useful Combinations

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# View file with line numbers and paging
cat -n file.txt | less

# Remove blank lines while viewing
cat -s document.txt

# Display multiple files with separators
cat file1 <(echo "---") file2

# Copy file contents (alternative to cp)
cat source.txt > destination.txt

Memory Efficiency

  • cat loads entire file into memory
  • For large files, consider streaming tools
  • Use split for processing huge files in chunks