Back to Blog

Linux cat Command Tutorial: View, Combine & Create Files [Beginner's Guide]

Sandy LaneSandy Lane

Video: Linux cat Command Tutorial: View, Combine & Create Files [Beginner's Guide] by Taught by Celeste AI - AI Coding Coach

Watch full page →

Linux cat Command Tutorial: View, Combine & Create Files [Beginner's Guide]

The cat command is a versatile Linux utility used to display, combine, and create text files directly from the terminal. This guide covers basic usage, advanced formatting options, and practical examples to help you manipulate file contents efficiently.

Code

# Display the contents of a single file
cat filename.txt

# Concatenate and display multiple files in sequence
cat file1.txt file2.txt

# Show line numbers with -n option
cat -n filename.txt

# Display non-printing characters (like tabs and line ends) with -v
cat -v filename.txt

# Squeeze multiple blank lines into one with -s
cat -s filename.txt

# Show all characters, including tabs (^I) and line ends ($), with -A
cat -A filename.txt

# Concatenate files and redirect output to a new file (overwrite)
cat file1.txt file2.txt > combined.txt

# Append contents of a file to an existing file
cat additional.txt >> combined.txt

# Create a new file using a here-document
cat << EOF > newfile.txt
This is line 1
This is line 2
EOF

# Pipe cat output to other commands, e.g., count lines with wc
cat filename.txt | wc -l

Key Points

  • cat displays file contents and can concatenate multiple files in order.
  • Options like -n, -v, -s, and -A enhance output readability and debugging.
  • Use redirection operators > and >> to create or append files from concatenated output.
  • Here-documents (<<) enable quick file creation with inline text input.
  • cat output can be piped to other commands for powerful text processing workflows.