cat — Concatenate and Display Files
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.
- 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
>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/htmlor/etc.
Core Syntax
cat [OPTION] [FILE...]
FILE...: one or more files to print in order- No file provided:
catreads 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
| Option | What it does | When you’d use it | Example |
|---|---|---|---|
| :- | :-- | :- | :- |
| (none) | Print file(s) to stdout | Quick viewing of small files | cat file.txt |
-n | Number all lines | Debugging with line references | cat -n file.txt |
-b | Number non-blank lines only | Cleaner numbering for formatted files | cat -b file.txt |
-s | Squeeze repeated blank lines | Normalize messy output | cat -s file.txt |
-A | Show non-printing chars (tabs/EOL/etc.) | Find hidden CRLF, tabs, trailing markers | cat -A file.txt |
-T | Show tabs as ^I | Debug Makefiles/YAML indentation | cat -T file.txt |
-E | Show $ at line ends | Spot line endings and trailing whitespace patterns | cat -E file.txt |
--help | Show help | Quick reference | cat --help |
--version | Show version | Confirm coreutils version | cat --version |
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
- View & Inspect
- Combine & Backup
- Create Files (Redirection)
- Pipelines
- WordPress VPS Quick Checks
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
cat index.php wp-config.php > combined.txt
cat /var/log/nginx/access.log /var/log/nginx/error.log > nginx_full.log
cat error.log >> all_errors.log
cat > notes.txt
Hello VPS world!
# Press CTRL+D to save and exit
cat <<EOF > wp-test.txt
Hello WordPress!
EOF
cat /var/www/html/wp-config.php | grep DB_NAME
head -n 20 /var/log/nginx/error.log | cat -n
zcat archive.gz | cat -n | head -n 10
cat /var/www/html/wp-config.php | grep DB_
cat /var/log/nginx/error.log | tail -n 50
cat -n /etc/nginx/sites-available/default | head -n 80
Best Practices
-
Use
catfor 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
catto “edit”—use an editor.
cat vs nano — when to use which?
- Use
catwhen you want to read/display file content, combine files, or use content in pipelines/scripts. It’s non-interactive and great for automation. - Use
nanowhen 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
| Problem | Likely Cause | Fix |
|---|---|---|
| : | :-- | : |
| Terminal floods / scrolls too fast | File is large | Use a pager: less file (or tail -n 200 file) |
| “Permission denied” | File requires elevated privileges | Use sudo cat /path/to/file (careful with sensitive configs) |
Output shows ^M or odd symbols | Windows CRLF or hidden chars | Use cat -A file.txt to confirm; convert line endings if needed |
| Overwrote a file by mistake | Used > instead of >> | Use >> for append; verify destination before redirecting |
| “No such file or directory” | Wrong path/typo | Confirm with ls -la and absolute paths |
| Gibberish output | Binary file printed to terminal | Inspect safely with tools like xxd (see examples) |
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
- What does
cat file1 file2 > file3do? - Which flag numbers only non-blank lines?
- How do you suppress repeated blank lines?
- What’s the difference between
>and>>? - Why is
lessoften better thancatfor huge log files?
Show answers
- Concatenates
file1andfile2and overwritesfile3with the result. -b-s>overwrites,>>appendslesspaginates 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.
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.
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.