Quick take: The cat command prints file contents to the terminal. Use it to view files, join them together, or create small files with redirection. Add -n to number lines and -A to reveal hidden characters.
Introduction
The cat command (short for concatenate) reads files and writes their contents to standard output. It is the quickest way to dump a file to the screen, and combined with redirection it can join files or create new ones.
This guide covers viewing files, numbering lines, revealing non-printing characters, and using cat with redirection — plus when to reach for less instead.
Syntax
The basic syntax of the cat command is:
cat [OPTIONS] [FILE...]Common Options and Parameters
The most useful options and parameters for the cat command:
| Option | Description |
|---|---|
| -n | Number all output lines. |
| -b | Number only non-blank lines. |
| -s | Squeeze multiple blank lines into one. |
| -A | Show all non-printing characters (tabs as ^I, line ends as $). |
| -E | Show a $ at the end of each line. |
| -T | Display tab characters as ^I. |
Practical Examples
Real cat commands you can run today:
# Print a file to the screen
cat notes.txt
# View a file with line numbers
cat -n script.sh
# Join two files into one
cat part1.txt part2.txt > whole.txt
# Append one file to another
cat extra.txt >> log.txt
# Create a small file from the terminal (Ctrl+D to finish)
cat > todo.txt
# Reveal hidden characters and line ends
cat -A config.confcat vs less, head, and tail
cat prints an entire file at once, which is perfect for small files but wrong for large ones — you lose the top before you can read it. Knowing the alternatives keeps you efficient:
less file— scroll a large file page by page, search with/, and quit withq.head -n 20 file— see just the first 20 lines.tail -n 20 file— see the last 20 lines;tail -ffollows a growing log live.
Reach for cat when you want the whole (small) file or need to concatenate; reach for less, head, or tail when the file is large or you only need part of it.
Common Mistakes and the Useless Use of cat
A frequent habit worth dropping is the “useless use of cat” — piping cat into another command that already accepts a filename. Writing cat file | grep error spawns an extra process for no benefit; grep error file is cleaner and faster. The same applies to wc, sort, and most text tools.
The other pitfall is using cat on a binary file, which floods the terminal with control characters and can leave it in a broken state. If that happens, run reset to restore it. To inspect a binary safely, use file to identify it or xxd to view a hex dump instead.
Tips and Best Practices
- For large files use
less filenameinstead —catdumps everything at once and you lose the top of the file. cat -Ais invaluable for spotting stray tabs, trailing spaces, or Windows line endings that break scripts and config files.- Avoid the “useless use of cat”: instead of
cat file | grep x, writegrep x file.
Final Thoughts
cat is the fastest way to see what is in a file and a handy tool for joining files together. Remember -n for line numbers and -A for debugging invisible characters, and switch to less when a file is too big to scroll past. It works hand in hand with grep, redirection, and the shell pipeline.
FAQ: cat Command in Linux
How do I view a file with cat?+
Run cat filename to print the whole file to the terminal. For long files, use less filename instead so you can scroll page by page.
How do I combine files with cat?+
List the files and redirect the output: cat file1 file2 > combined writes both into a new file. Use >> to append to an existing file.
How do I show line numbers with cat?+
Use cat -n to number every line, or cat -b to number only non-blank lines.
How do I create a file with cat?+
Run cat > newfile.txt, type your content, then press Ctrl+D to save and exit. This is handy for quick notes without opening an editor.
Why should I avoid cat file | command?+
Piping cat into another command is unnecessary because most tools accept a filename directly. Writing grep pattern file is cleaner and faster than cat file | grep pattern.
Need help with Linux servers or infrastructure?
Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.
Hire Me for Support