cat Command Cheat Sheet
cat (short for concatenate) is one of the most frequently used commands in Unix-like operating systems. It reads data from files and outputs their contents. It is commonly used to display files, combine copies of files, and create new files through redirection.
Synopsis
cat [OPTION]... [FILE]...
Description
The cat command reads files sequentially, writing them to standard output. If no file is specified, or if - is used as a filename, cat reads from standard input.
Basic Usage
Display File Contents
cat filename.txt
Prints the contents of filename.txt to the terminal.
Concatenate Multiple Files
cat file1.txt file2.txt
Prints content of file1.txt followed by file2.txt.
Create a New File
cat > newfile.txt
Type your text, then press Ctrl+D to save and exit.
Append to Existing File
cat >> existing.txt
Appends typed text to the end of existing.txt. Press Ctrl+D to finish.
Options
-n, --number
Number all output lines (starting at 1).
cat -n script.sh
-b, --number-nonblank
Number nonempty output lines, overrides -n.
cat -b script.sh
-s, --squeeze-blank
Suppress repeated empty output lines.
cat -s wide_spaced_file.txt
Reduces multiple adjacent blank lines to a single blank line.
-v, --show-nonprinting
Display non-printing characters (except LFD and TAB).
cat -v binary_file
Useful for debugging weird characters in text files.
- Control characters printed as ^C
- Meta characters printed as M-
-E, --show-ends
Display $ at end of each line.
cat -E file.txt
Helps identify trailing whitespace.
-T, --show-tabs
Display TAB characters as ^I.
cat -T Makefile
Essential for debugging Makefiles where tabs vs spaces matter.
-A, --show-all
Equivalent to -vET. Shows all non-printing characters, tabs, and line endings.
cat -A suspicious_file.txt
Advanced Usage
Merging Files
Combine multiple files into one:
cat part1.txt part2.txt part3.txt > complete_book.txt
Displaying Large Files
While cat dumps the whole file, for large files use pager:
cat large_file.log | less
Using Standard Input
Read from keyboard (stdin) and a file:
cat - file.txt
Type some text, press Ctrl+D, then it prints file.txt.
Numbering Lines in Stream
ls -1 | cat -n
Heredocs (Here Documents)
A powerful way to create files in scripts using cat.
Basic Heredoc
cat <<EOF > output.txt
This is line 1.
This is line 2.
Variables like $HOME are expanded.
EOF
Append with Heredoc
cat <<EOF >> output.txt
Appended line.
EOF
Suppress Variable Expansion
Use quotes around the delimiter to prevent expansion:
cat <<'EOF' > script.sh
echo "Current directory is \$PWD"
# $PWD is NOT expanded here
EOF
Indented Heredoc (Tabs)
Use <<- to strip leading tabs (but not spaces):
cat <<-EOF
This line has leading tabs stripped.
This one too.
EOF
Practical Examples
Concatenate Log Files
Merge daily logs into a monthly log:
cat access.log.* > access_month.log
Check for Hidden Characters
If a script is failing due to Windows line endings (\r\n):
cat -v script.sh
# Output might show: #!/bin/bash^M
^M indicates carriage return (CR).
Reverse File Content
Use tac (reverse cat):
tac file.txt
Prints lines in reverse order (last line first).
Create Dummy File
cat /dev/urandom | head -c 100 > random_bytes.bin
Copy File Content to Clipboard (macOS/Linux)
# macOS
cat file.txt | pbcopy
# Linux (with xclip)
cat file.txt | xclip -selection clipboard
Display File with Line Numbers for Code Review
cat -n code.py
Alternatives
tac
Reverse concatenate (lines).
tac file.txt
rev
Reverse characters in each line.
rev file.txt
head / tail
View beginning or end of file.
head -n 5 file.txt
tail -f file.txt
less / more
Interactive pagers.
less file.txt
bat
A modern cat clone with syntax highlighting and Git integration.
bat file.txt
Common Mistakes
Useless Use of Cat (UUoC)
Piping cat into a command that accepts file arguments is redundant.
Bad:
cat file.txt | grep "pattern"
Good:
grep "pattern" file.txt
Bad:
cat file.txt | awk '{print $1}'
Good:
awk '{print $1}' file.txt
Exception: When you want to construct a stream from multiple sources.
cat header.txt body.txt footer.txt | grep "TODO"
Overwriting Input File
Dangerous:
cat file.txt > file.txt
This truncates file.txt to zero bytes before reading it! The file becomes empty.
Safe way:
cat file.txt > temp.txt && mv temp.txt file.txt
# Or use sponge from moreutils
cat file.txt | sponge file.txt
Exit Status
| Code | Meaning |
|---|---|
| 0 | Success |
| 1 | Error (file not found, permission denied) |
Tips and Best Practices
- Use Redirection -
>for new/overwrite,>>for append - Watch Out for Large Files -
catdumps everything;Ctrl+Cto stop - Use
-vfor Debugging - Finds hidden control characters - Heredocs are Awesome - Use them in scripts for config generation
- Standard Input -
catwithout args reads from keyboard; useful for testing pipes - Binaries - Don't
catbinary files to terminal (can mess up display encoding) - Reset Terminal - If you cat a binary and terminal garbles, type
reset