CIS120 Linux Fundamentals by Scott Shaper

The cat Command

Think of the cat command as your Swiss Army knife for working with text files in Linux. It's short for "concatenate" (which means to join things together), but it can do much more than just combine files. You can use it to view file contents, create new files, and even add text to existing files.

Why Learn the cat Command?

The cat command is essential because:

Basic Syntax

cat [options] [file1] [file2] ...

Common Options

Option What It Does When to Use It
-n Numbers all lines When you need to reference specific lines
-v Shows non-printing characters When you need to see special characters
-b Numbers only non-blank lines When you want to ignore empty lines
-s Removes extra blank lines When your file has too many empty lines
-E Shows $ at the end of each line When you need to see line endings
-T Shows tabs as ^I When you need to see tab characters
-A Shows all special characters When debugging file formatting

Practical Examples

Viewing a File

The most basic use of cat is to display a file's contents:

cat notes.txt

This shows everything in notes.txt on your screen.

Viewing Multiple Files

You can view several files at once:

cat file1.txt file2.txt

This shows the contents of both files, one after the other.

Creating a New File

To create a new file and type its contents:

cat > newfile.txt
Type your text here
Press Ctrl+D when done

This creates newfile.txt with whatever you type. Press Ctrl+D to save and exit.

Adding to a File

To add text to the end of an existing file:

cat >> existing.txt
This text will be added to the end
Press Ctrl+D when done

This adds your new text to the end of existing.txt.

You can also create mutli-line files with the << 'EOF' syntax. This is a here document. It is a way to create a file with multiple lines of text.

cat > newfile.txt << 'EOF'
This is the first line
This is the second line
This is the third line
EOF

This creates newfile.txt with the three lines. NOTE: When using the << 'EOF' syntax, you do not need to use the Ctrl+D command to save and exit. The EOF is the end of the file just press the enter key to save and exit.

Viewing with Line Numbers

To see a file with line numbers:

cat -n notes.txt

This shows each line with its number, making it easier to reference specific parts.

Viewing Special Characters

To see all special characters (like tabs and line endings):

cat -A notes.txt

This shows:

  • $ at the end of each line
  • ^I for tab characters
  • Other special characters

Viewing Non-Printing Characters

Sometimes text files contain characters that you can't see normally, like special formatting or control codes. The -v option makes these invisible characters visible:

cat -v notes.txt

For example, if your file contains:

  • A tab character, it will show as ^I
  • A Windows-style line ending, it will show as ^M
  • Special characters like © or é, they will show as M- followed by a code

Note: The -A option is actually more comprehensive than -v. It's equivalent to using -vET (showing non-printing characters, line endings with $, and tabs as ^I). Use -A when you want to see everything, or -v when you only need to see non-printing characters without the line ending $ symbols.

Practice Exercises

Try these exercises to practice using the cat command. Create a practice directory first with mkdir practice_cat and then cd practice_cat to work through these exercises.

First, create these practice files by running these commands

NOTE: The << 'EOF' is used to create a here document. It is a way to create a file with multiple lines of text.

Create a simple two-line file:

cat > hello.txt << 'EOF'
Hello, World!
This is my first file.
EOF

Create a single-line file:

cat > file1.txt << 'EOF'
First file
EOF

Create another single-line file:

cat > file2.txt << 'EOF'
Second file
EOF

Create a file with blank lines between text:

cat > mixed.txt << 'EOF'
Line one

Line three

Line five
EOF

Create a file with a tab character between words:

cat > special.txt << 'EOF'
Word1	Word2
EOF

Create a file with multiple consecutive blank lines:

cat > spaces.txt << 'EOF'
First line


Second line


Third line
EOF

Create a header file for the report exercise:

cat > header.txt << 'EOF'
Header
Title: My Report
EOF

Create a body file for the report exercise:

cat > body.txt << 'EOF'
Body content here
More details
EOF

Create a footer file for the report exercise:

cat > footer.txt << 'EOF'
Footer
End of report
EOF

Create a file with mixed formatting (tabs and spaces) for debugging:

cat > debug.txt << 'EOF'
Line with tab	here
Line with    spaces
Normal line
EOF

Now practice these cat commands:

  1. Basic file viewing
    • cat hello.txt - View the contents of hello.txt
    • cat file1.txt - View file1.txt
    • cat file2.txt - View file2.txt
  2. View files with line numbers
    • cat -n hello.txt - View hello.txt with line numbers
    • cat -n file1.txt - View file1.txt with line numbers
    • What line number is "Hello, World!" on?
  3. View multiple files together
    • cat file1.txt file2.txt - View both files at once
    • cat hello.txt file1.txt file2.txt - View all three files
    • Notice how the contents are displayed one after the other
  4. Practice with line numbering options
    • cat -n mixed.txt - See all lines numbered (including blank lines)
    • cat -b mixed.txt - See only non-blank lines numbered
    • Compare the output of both commands. What's the difference?
  5. View special characters
    • cat special.txt - View it normally
    • cat -A special.txt - See the tab character displayed as ^I
    • cat -T special.txt - See only tabs as ^I
    • cat -E special.txt - See only line endings with $
    • Notice the differences between these options
  6. Combine files into a new file
    • cat file1.txt file2.txt > combined.txt - Combine the two files into a new file
    • cat combined.txt - Verify the contents
    • cat -n combined.txt - See the combined file with line numbers
  7. Practice with the -s option
    • cat spaces.txt - See it normally with multiple blank lines
    • cat -s spaces.txt - See how multiple blank lines are reduced to single blank lines
    • Compare the output - notice the difference?
  8. Debug formatting issues
    • cat debug.txt - View the file normally
    • cat -A debug.txt - View all special characters
    • Identify where tabs (^I) and line endings ($) appear
    • cat -v debug.txt - View non-printing characters
    • Compare -A and -v outputs
  9. Add content to an existing file
    • cat >> hello.txt - Then type "This line was added later." and press Enter, then Ctrl+D
    • cat hello.txt - Verify the new line was added to the end
    • cat -n hello.txt - View with line numbers to see the new line
  10. Challenge: Create a formatted report
    • cat header.txt body.txt footer.txt > report.txt - Combine all three into report.txt
    • cat report.txt - View the complete report
    • cat -n report.txt - View with line numbers
    • cat >> report.txt - Then type "Generated: $(date)" and press Enter, then Ctrl+D (or just type a timestamp manually)
    • cat report.txt - Verify the timestamp was added

Tips for completing these exercises:

Tips for Success

Common Mistakes to Avoid

Best Practices