Skip to main content

cat — Concatenate and Display Files

Command GNU coreutils VPS / WordPress
Learning Focus

By the end of this lesson, you’ll confidently use cat to view, combine, and write files via redirection, plus apply it in WordPress VPS workflows (configs, logs, backups) without accidentally overwriting important data.

Overview

cat (catenate) reads one or more files in sequence and writes them to standard output (your terminal). With redirection (> / >>) and pipes (|), it becomes a core building block for inspection, automation, and quick file assembly.

Tool Snapshot
  • Core Function: Print file contents (or stdin) to stdout.
  • Primary Benefit: Fast viewing + easy composition with pipes and redirection.
  • Where to Use: Config checks (wp-config.php), log inspection, quick merges, scripting.
  • Workflow: cat [OPTION]... [FILE]... (then optionally |, >, >>).

System Check

Verify cat is installed and check version:

which cat # Expected: /usr/bin/cat
cat --version # Expected: GNU coreutils version output

Mental Model

Think of cat as a stream copier:

  • Input: one or more files (or stdin if no file is provided)
  • Output: stdout by default
  • You can redirect stdout to a file or pipe it into another command
Safety Note: Redirection
  • > overwrites the destination file immediately (even if the command later fails).
  • >> appends to the destination file. Always double-check your target path—especially under /var/www/html or /etc.

Core Syntax

cat [OPTION] [FILE...]
  • FILE...: one or more files to print in order
  • No file provided: cat reads from stdin (useful in scripts and pipelines)

Fast read examples (syntax only)

cat file.txt
cat file1.txt file2.txt
cat -n file.txt

Options & Flags

OptionWhat it doesWhen you’d use itExample
:-:--:-:-
(none)Print file(s) to stdoutQuick viewing of small filescat file.txt
-nNumber all linesDebugging with line referencescat -n file.txt
-bNumber non-blank lines onlyCleaner numbering for formatted filescat -b file.txt
-sSqueeze repeated blank linesNormalize messy outputcat -s file.txt
-AShow non-printing chars (tabs/EOL/etc.)Find hidden CRLF, tabs, trailing markerscat -A file.txt
-TShow tabs as ^IDebug Makefiles/YAML indentationcat -T file.txt
-EShow $ at line endsSpot line endings and trailing whitespace patternscat -E file.txt
--helpShow helpQuick referencecat --help
--versionShow versionConfirm coreutils versioncat --version
Heads-up: “cat is often optional”

Many cat | ... patterns work, but the left-side cat is sometimes redundant (e.g., less bigfile.txt instead of cat bigfile.txt | less). It’s still useful when you need to combine multiple files or force stdin behavior.

Common Patterns

cat /var/www/html/wp-config.php
cat -n /var/www/html/wp-config.php | head -n 40
cat -A /var/www/html/wp-config.php | head -n 20

Best Practices

  • Use cat for small files and quick output; use a pager for large files:

    less /var/log/nginx/error.log
  • Prefer numbered output when debugging:

    cat -n file.txt
  • Use hidden-character flags when configs behave oddly (CRLF, tabs, invisible chars):

    cat -A file.txt
  • For safe file writes, avoid accidental overwrite:

    • If you mean append, use:

      cat file.txt >> target.txt
    • If overwriting is intended, verify destination first:

      ls -l target.txt
  • Don’t use cat to “edit”—use an editor.

cat vs nano — when to use which?
  • Use cat when you want to read/display file content, combine files, or use content in pipelines/scripts. It’s non-interactive and great for automation.
  • Use nano when you want to edit a file interactively (change config values, update scripts).

Pros / Cons

  • cat

    • Fast, script-friendly, perfect for piping
    • Not an editor, not ideal for huge files (can flood terminal)
  • nano

    • Beginner-friendly interactive editing
    • Not script-friendly (interactive), can be slow on very large files

Example: check DB name without opening an editor:

cat /var/www/html/wp-config.php | grep DB_NAME

Example: edit config safely:

nano /var/www/html/wp-config.php

Troubleshooting

ProblemLikely CauseFix
::--:
Terminal floods / scrolls too fastFile is largeUse a pager: less file (or tail -n 200 file)
“Permission denied”File requires elevated privilegesUse sudo cat /path/to/file (careful with sensitive configs)
Output shows ^M or odd symbolsWindows CRLF or hidden charsUse cat -A file.txt to confirm; convert line endings if needed
Overwrote a file by mistakeUsed > instead of >>Use >> for append; verify destination before redirecting
“No such file or directory”Wrong path/typoConfirm with ls -la and absolute paths
Gibberish outputBinary file printed to terminalInspect safely with tools like xxd (see examples)
Destructive Pitfall: Redirecting Into the Same File

Avoid writing output back into a source file you’re reading from (e.g., cat file > file). This can truncate the file.

Cheat Sheet

cat file.txt # Display file
cat file1 file2 # Display files in sequence
cat file1 file2 > combined.txt # Overwrite combined.txt with merged content
cat file1 >> combined.txt # Append file1 to combined.txt
cat -n file.txt # Number all lines
cat -b file.txt # Number non-blank lines
cat -s file.txt # Squeeze repeated blank lines
cat -A file.txt # Show hidden/non-printing characters
cat -T file.txt # Show tabs as ^I
cat -E file.txt # Show $ at line ends

Mini Quiz

  1. What does cat file1 file2 > file3 do?
  2. Which flag numbers only non-blank lines?
  3. How do you suppress repeated blank lines?
  4. What’s the difference between > and >>?
  5. Why is less often better than cat for huge log files?
Show answers
  1. Concatenates file1 and file2 and overwrites file3 with the result.
  2. -b
  3. -s
  4. > overwrites, >> appends
  5. less paginates output so you can scroll/search without flooding the terminal.

Worked Examples

1) View a WordPress config file

cat /var/www/html/wp-config.php

Expected output: (prints the full contents of wp-config.php) Use case: Inspect WordPress configuration values.

2) Display multiple files in sequence

cat index.php wp-config.php

Expected output: (prints index.php, then wp-config.php) Use case: Quick concatenation to screen.

3) Merge files into a new file (overwrite)

cat index.php wp-config.php > combined.txt

Expected output: (no output if successful) Use case: Create a combined bundle.

Overwrite Warning

If combined.txt already exists, it will be replaced.

4) Append output to an existing file

cat error.log >> all_errors.log

Expected output: (no output if successful) Use case: Append new log entries into a master log.

5) Create a new file from keyboard input (stdin)

cat > notes.txt
Hello VPS world!
# Press CTRL+D to save and exit

Expected output: (no output; file is created/updated) Use case: Quick note-taking in shell without an editor.

6) Number all lines

cat -n file.txt

Expected output:

1 First line
2 Second line

Use case: Debugging with line references.

7) Number only non-blank lines

cat -b file.txt

Expected output:

1 First line

2 Another line

Use case: Cleaner numbering when files include blank spacing.

8) Squeeze multiple blank lines

cat -s file.txt

Expected output: (repeated blank lines are reduced to a single blank line) Use case: Normalize messy output.

9) Show hidden characters (tabs, line ends, etc.)

cat -A file.txt

Expected output (example):

Line1$
Line2^Itext$

Use case: Spot tabs (^I), end markers ($), and other non-printing chars.

10) Show tabs explicitly

cat -T file.txt

Expected output: (tabs appear as ^I) Use case: Debug Makefiles or indentation-sensitive files.

11) Show line endings

cat -E file.txt

Expected output (example):

Line1$
Line2$

Use case: Verify line ends and detect formatting issues.

12) Number the first 20 log lines (head + cat)

head -n 20 /var/log/nginx/error.log | cat -n

Expected output: (first 20 lines, numbered) Use case: Quick triage with line numbers.

13) Filter a config value using grep

cat /var/www/html/wp-config.php | grep DB_NAME

Expected output (example):

define('DB_NAME', 'wordpress');

Use case: Confirm WordPress database name quickly.

14) Preview a database dump header

cat db_backup.sql | head -n 5

Expected output: (first 5 lines of the SQL dump) Use case: Confirm the dump looks valid.

15) Combine Nginx logs into one archive file

cat /var/log/nginx/access.log /var/log/nginx/error.log > nginx_full.log

Expected output: (no output if successful) Use case: Single-file export for troubleshooting.

16) Inspect the first bytes of a binary (safe-ish preview)

cat /bin/ls | head -c 20 | xxd

Expected output: (hex dump of first 20 bytes) Use case: Quick header peek without dumping the entire binary.

17) Create a file with heredoc (script-friendly)

cat <<EOF > wp-test.txt
Hello WordPress!
EOF

Expected output: (no output; file created) Use case: Generate config snippets in scripts.

18) Page through a large file (better alternative shown)

cat bigfile.txt | less

Expected output: (opens a pager view) Use case: Scroll/search large content.

Better

Most of the time, you can skip cat entirely:

less bigfile.txt

19) Compare a file to another using stdin with diff

cat nginx.conf | diff -u another_nginx.conf -

Expected output: (unified diff if files differ; nothing if identical) Use case: Compare generated/streamed content to a reference file.

20) Preview a compressed file with numbering

zcat archive.gz | cat -n | head -n 10

Expected output: (first 10 lines, numbered) Use case: Quick preview of compressed logs without full extraction.