cat
Concatenate and print file contents
By CMD Script Team · 3 min read · Last updated
cat [OPTIONS] [FILE...]Options
| Flag | Description |
|---|---|
-n | Number all output lines, starting at 1 |
-b | Number only non-blank output lines |
-A | Show non-printing characters and line ends (equivalent to -vET) |
-s | Squeeze multiple adjacent blank lines into a single blank line |
-E | Display a $ at the end of each line |
> | Redirect stdout to a file, used with cat to create a file from typed input |
Distribution compatibility
- Ubuntu
- Debian
- Fedora
- Arch
- macOS
What it does
cat (short for "concatenate") reads one or more files in sequence and writes their
contents to standard output. With a single file it simply prints it; with multiple files
it prints them one after another, effectively joining them. It's also commonly used with
shell redirection to build files from typed input or to combine several files into one.
Beginner examples
cat file.txt— print a file's contents to the terminalcat file1.txt file2.txt— print two files back to backcat -n file.txt— print with line numberscat > newfile.txt— type content interactively, saved with Ctrl+D
cat /etc/os-release
Advanced examples
- Join multiple files into a new one:
cat part1.log part2.log part3.log > full.log - Append rather than overwrite when combining:
cat extra.log >> full.log - Number only non-blank lines, useful for reviewing scripts:
cat -b script.sh - Reveal hidden whitespace issues (tabs as
^I, line ends as$):cat -A file.txt - Squeeze repeated blank lines when reading a messy file:
cat -s report.txt
cat header.txt body.txt footer.txt > document.txt
Common mistakes
- Using
cat file | grep patternwhengrep pattern filedoes the same thing with one fewer process (the classic "useless use of cat"). - Redirecting a file into itself, e.g.
cat file.txt > file.txt, which truncates the file to empty beforecatever reads it, destroying the content. - Running plain
caton a large binary file and flooding the terminal with garbage characters, sometimes even breaking the terminal's display settings. - Forgetting that
cat > file.txtoverwrites an existing file entirely; use>>to append instead.
Tips
- If your terminal display gets garbled after accidentally
cat-ing a binary file, runresetto restore it. - Use
cat -eorcat -Awhen debugging files with mysterious formatting issues, such as trailing whitespace or Windows-style line endings (^M$). - For quickly viewing large files, prefer
less—catdumps everything at once with no way to scroll back.
Best practices
- Avoid the "useless use of cat" anti-pattern in scripts and pipelines; pass the filename
directly to the next command (
grep pattern fileinstead ofcat file | grep pattern) when only one file is involved. - Use
catfor genuinely concatenating multiple files or dumping small files quickly, not as a general-purpose file viewer for large files. - When building files programmatically, prefer heredocs (
cat <<EOF) over interactivecat >for scripts, since heredocs are non-interactive and reproducible.
Try it yourself
A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.
Real-world use cases
- Combining log rotation parts into a single file for analysis:
cat access.log.1 access.log.2 > combined.log. - Quickly creating a small config file without opening an editor:
cat > .env.local. - Dumping a script's contents with line numbers to reference specific lines in a bug
report:
cat -n deploy.sh.
Common interview questions
- What does "useless use of cat" mean? It refers to piping
cat file | commandwhencommandcould read the file directly (command file), adding an unnecessary extra process. - How do you combine multiple files into one with cat?
cat file1 file2 file3 > combined.txt— each file's contents are written to stdout in order and redirected into the new file. - What happens if you run
cat file.txt > file.txt? The shell truncatesfile.txtto empty as soon as it opens the redirection target, socatreads an already-empty file and you lose the original contents.
Frequently Asked Questions
How do I create a file with cat without an editor?
Run cat > file.txt, type the content, then press Ctrl+D on its own line to save and exit. This reads from stdin until end-of-file.
How do I combine multiple files into one?
Use cat file1 file2 file3 > combined.txt. cat writes each file's contents in order to stdout, which you redirect into the new file.
Why does 'cat' with no arguments seem to hang?
With no file argument, cat reads from standard input, which is your terminal by default. It will wait for typed input until you send EOF with Ctrl+D, or you can cancel with Ctrl+C.
Is cat safe to use on binary files?
It won't corrupt the file, but printing binary data to a terminal can dump control characters that mess up your terminal's display. Use file first to check, or use less/xxd for binary content.
Cheat sheet
Download a quick-reference cheat sheet for cat.