cat Cheatsheet
Quick reference for viewing, combining, and redirecting file content with cat in Linux
The `cat` command prints and concatenates file content in Linux. This cheatsheet covers common viewing patterns, line-number options, safe redirection, and practical command combinations.
Basic Syntax
Core cat command forms.
| Command | Description |
|---|---|
cat FILE | Print a file to standard output |
cat FILE1 FILE2 | Print multiple files in sequence |
cat | Read from standard input until EOF |
cat -n FILE | Show all lines with line numbers |
cat -b FILE | Number only non-empty lines |
View File Content
Common read-only usage patterns.
| Command | Description |
|---|---|
cat /etc/os-release | Print distro information file |
cat file.txt | Show full file content |
cat file1.txt file2.txt | Show both files in one stream |
cat -A file.txt | Show tabs/end-of-line/non-printing chars |
cat -s file.txt | Squeeze repeated blank lines |
Line Numbering and Visibility
Inspect structure and hidden characters.
| Command | Description |
|---|---|
cat -n file.txt | Number all lines |
cat -b file.txt | Number only non-blank lines |
cat -E file.txt | Show $ at end of each line |
cat -T file.txt | Show tab characters as ^I |
cat -v file.txt | Show non-printing characters |
Combine Files
Create merged outputs from multiple files.
| Command | Description |
|---|---|
cat part1 part2 > merged.txt | Merge files into a new file |
cat header.txt body.txt footer.txt > report.txt | Build one file from sections |
cat a.txt b.txt c.txt > combined.txt | Join several text files |
cat file.txt >> archive.txt | Append one file to another |
cat *.log > all-logs.txt | Merge matching files (shell glob) |
Create and Append Text
Use cat with redirection and here-docs.
| Command | Description |
|---|---|
cat > notes.txt | Create/overwrite a file from terminal input |
cat >> notes.txt | Append terminal input to a file |
cat <<'EOF' > config.conf | Write multiline text safely with a here-doc |
cat <<'EOF' >> config.conf | Append multiline text using a here-doc |
cat > script.sh <<'EOF' | Create a script file from inline content |
Pipelines and Common Combos
Practical command combinations with other tools.
| Command | Description |
|---|---|
cat access.log | grep 500 | Filter matching lines |
cat file.txt | wc -l | Count lines |
cat file.txt | head -n 20 | First 20 lines |
cat file.txt | tail -n 20 | Last 20 lines |
cat file.txt | tee copy.txt >/dev/null | Copy stream to a file |
Troubleshooting
Quick checks for common cat usage issues.
| Issue | Check |
|---|---|
Permission denied | Check ownership and file mode with ls -l; run with correct user or sudo if required |
| Output is too long | Pipe to less (cat file | less) or use head/tail |
| Unexpected binary output | Verify file type with file filename before printing |
| File was overwritten accidentally | Use >> to append, not >; enable shell noclobber if needed |
| Hidden characters break scripts | Inspect with cat -A or cat -vET |
Related Guides
Use these guides for full file-view and text-processing workflows.
| Guide | Description |
|---|---|
| How to Use the cat Command in Linux | Full cat command guide |
| head Command in Linux | Show the first lines of files |
| tail Command in Linux | Follow and inspect recent lines |
| tee Command in Linux | Write output to file and terminal |
| wc Command in Linux | Count lines, words, and bytes |
| Create a File in Linux | File creation methods |
| Bash Append to File | Safe append patterns |