cat Command in Linux With Examples

Use the cat command in Linux to view, join, and inspect text files. Covers line numbers, hidden characters, pipes, and errors.

PublishedAuthorJoshua JamesRead time10 minGuide typeLinux Commands

Small text files are fastest to inspect when you can read them without opening an editor, but cat is more than a quick viewer. The cat command in Linux prints files to standard output, joins multiple inputs in order, and feeds file content into redirection or pipelines when you need a simple text stream.

Most general-purpose Linux distributions provide cat through GNU Coreutils or uutils Coreutils, while minimal systems may use a BusyBox applet with a smaller help and version surface. The examples focus on behavior shared by common GNU/uutils systems; on minimal systems, check local help before relying on less-common options.

Understand the cat Command in Linux

cat reads each file operand from left to right and writes the bytes to standard output. If no file is supplied, or if an operand is -, cat reads from standard input instead. That behavior makes it useful for quick viewing, file joining, simple redirection, and pipelines that expect a stream.

cat Command Syntax

The basic syntax accepts options first, followed by one or more files:

cat [OPTION]... [FILE]...
  • OPTION changes output formatting, such as line numbering, blank-line squeezing, or visible tabs and line endings.
  • FILE can be one file, several files, - for standard input, or a shell-expanded pattern such as chapter-*.txt.
  • With no FILE, cat waits for input from the terminal until you send end-of-file with Ctrl+D or stop it with Ctrl+C.

cat Command Quick Reference

TaskCommand PatternWhat It Does
Show one filecat file.txtPrints the file to standard output.
Read a name with spacescat 'report final.txt'Quotes the path so the shell passes it as one filename.
Join several filescat part1.txt part2.txtPrints each file in the order supplied.
Write joined outputcat part1.txt part2.txt > combined.txtCreates or overwrites the destination with the combined stream.
Append joined outputcat part1.txt >> combined.txtAdds the stream to the end of the destination file.
Add a separator{ cat part1.txt; printf '\n---\n'; cat part2.txt; }Prints a visible boundary between file contents.
Read from standard inputprintf 'text\n' | catCopies pipeline input to standard output.
Insert standard input between filescat intro.txt - body.txtReads intro.txt, then standard input, then body.txt.
Number all linescat -n file.txtAdds line numbers, including blank lines.
Number nonblank linescat -b file.txtAdds numbers only to nonempty lines and overrides -n.
Squeeze repeated blank linescat -s file.txtCollapses adjacent blank lines to one blank line in the output.
Show tabs and line endingscat -A file.txtMakes tabs, nonprinting characters, and line ends visible.
Reassemble split partscat payload.part-* > payload-restored.txtCombines ordered chunks into one file.

Verify cat Availability and Implementation

Check the resolved command path when a script or troubleshooting step needs to prove which executable the shell will run:

command -v cat

A general-purpose Linux install returns a path such as:

/usr/bin/cat

On GNU and uutils systems, use a version check when option behavior matters:

cat --version | head -n 1

GNU builds usually start with cat (GNU coreutils), while uutils builds start with cat (uutils coreutils). If a minimal BusyBox system rejects --version, use local cat --help or busybox cat --help output before relying on uncommon behavior. The GNU Coreutils cat manual and the uutils cat documentation are the safest upstream references for the full implementations.

Set Up Example Files

A disposable directory keeps output predictable. Create it in your home directory and move into it:

mkdir -p ~/cat-demo/docs
cd ~/cat-demo
printf 'Project notes\n' > intro.txt
printf 'Install package\nVerify service\nRestart service\n' > steps.txt
printf 'alpha\n\n\nbeta\n' > spacing.txt
printf 'name\tstatus\napi\trunning\n' > tabs.txt
printf 'inside docs\n' > docs/example.txt
printf 'chapter one\n' > chapter-1.txt
printf 'chapter two\n' > chapter-2.txt
printf 'footer\n' > footer.txt
printf 'dash file\n' > ./-dash.txt
printf 'single dash file\n' > ./-
printf 'spaced name\n' > 'report final.txt'
printf 'no newline' > no-newline.txt
printf 'bell:\a\n' > control.txt
printf 'windows\r\nnext\r\n' > crlf.txt
printf 'abcdef' > payload.txt

The files cover normal text, blank lines, tab characters, a nested file, filenames with shell-sensitive shapes, CRLF line endings, and one file without a final newline.

Use Basic cat Command Examples

Display One File

Print a small file directly to the terminal:

cat intro.txt
Project notes

cat is best for short files where seeing the whole content at once is useful. For long files, use a pager such as less or inspect the newest lines with tail command examples.

Display Multiple Files in Order

Pass several files when you want one continuous stream:

cat intro.txt steps.txt
Project notes
Install package
Verify service
Restart service

cat does not insert headings, blank lines, or separators between files. If a boundary matters, add it to the source files or print a separator yourself before combining output.

Display a Filename with Spaces

Quote filenames that contain spaces, tabs, glob characters, or other shell-sensitive characters. Without quotes, the shell splits the name before cat receives it:

cat 'report final.txt'
spaced name

This is a shell quoting issue, not a special cat option. Use quotes for the exact path instead of escaping only the first space and hoping the rest of the command survives later edits.

Read Standard Input from a Pipeline

When no file operand is supplied, cat copies standard input to standard output:

printf 'from stdin\n' | cat
from stdin

This form is simple, but many commands can already read files or standard input directly. Use cat in a pipeline when it makes the stream clearer, not by habit.

Insert Standard Input Between Files

The - operand means standard input at that exact point in the file order:

printf 'inserted stdin\n' | cat intro.txt - steps.txt
Project notes
inserted stdin
Install package
Verify service
Restart service

This is useful when a generated header, prompt answer, or command output needs to sit between existing files without creating a temporary file first.

Number and Inspect Text with cat Options

Number Every Line

Use -n when every physical line needs a number:

cat -n steps.txt
     1	Install package
     2	Verify service
     3	Restart service

Line numbering is display-only. It does not edit the file or store the numbers unless you redirect the output to a different file.

Number Only Nonblank Lines

Use -b when blank lines should stay unnumbered:

cat -b spacing.txt
     1	alpha


     2	beta

If -b and -n are both supplied, -b wins on GNU-compatible and uutils implementations. That makes cat -bn file behave like nonblank line numbering.

Squeeze Repeated Blank Lines

Use -s to collapse adjacent blank lines in the displayed output:

cat -s spacing.txt
alpha

beta

The file still contains the original blank lines. Redirect to a new file only when you intentionally want to save the squeezed output.

Show Line Endings

Use -E when whitespace or missing final newlines are the suspected problem:

cat -E spacing.txt
alpha$
$
$
beta$

The dollar sign marks where each line ends. It is not part of the file content.

Detect Windows CRLF Line Endings

Use -A when a script, config file, or data file looks correct but still behaves as if hidden characters are present:

cat -A crlf.txt
windows^M$
next^M$

The ^M marker shows a carriage return before the line feed. That usually means the file has Windows-style CRLF endings instead of Linux LF endings.

Show Tabs and Nonprinting Characters

Use -T when tabs are hard to distinguish from spaces:

cat -T tabs.txt
name^Istatus
api^Irunning

Use -A for a fuller diagnostic view that combines visible tabs, line endings, and nonprinting character notation:

cat -A tabs.txt
name^Istatus$
api^Irunning$

For one nonprinting control character, -v displays caret notation while leaving normal line feeds and tabs alone:

cat -v control.txt
bell:^G

These options help identify formatting problems in copied configuration files, generated reports, CSV/TSV data, and scripts where invisible characters change behavior.

Combine and Create Files with cat

Combine Files into a New File

Redirect the combined stream when you want to save it:

cat intro.txt steps.txt > combined.txt
cat combined.txt
Project notes
Install package
Verify service
Restart service

The > redirection operator overwrites the destination before cat writes to it. Do not use the same file as both input and output, and use >> when the goal is append-only output.

Add Separators Between Combined Files

Print a separator yourself when readers need to see where one file ends and the next begins:

{ cat intro.txt; printf '\n---\n'; cat steps.txt; } > separated.txt
cat separated.txt
Project notes

---
Install package
Verify service
Restart service

The braces group the stream before redirection, so the destination receives the first file, the separator, and the second file as one ordered output.

Append One File to Another

Use append redirection when the existing destination content must remain:

cat footer.txt >> combined.txt
tail -n 2 combined.txt
Restart service
footer

The appended file starts immediately after the existing content. If the destination does not end with a newline, the first appended line can join the previous line.

Add a Newline Before Appending

Add an explicit newline first when the destination might not already end cleanly:

cp no-newline.txt appended-safe.txt
printf '\n' >> appended-safe.txt
cat footer.txt >> appended-safe.txt
cat -E appended-safe.txt
no newline$
footer$

The -E check confirms that each logical line now ends before the next file content starts.

Create a Short File from Standard Input

Interactive cat > file.txt works, but a here document is easier to repeat and review in documentation or scripts:

cat > created-with-cat.txt <<'EOF'
first note
second note
EOF
cat created-with-cat.txt
first note
second note

Use a real editor for longer files. cat has no editing buffer, undo history, syntax checks, or confirmation prompt before redirection writes the destination.

Write Root-Owned Content with sudo tee

Shell redirection happens before sudo changes privileges, so sudo cat > /path/file does not make the destination writable. Pipe the generated content into sudo tee when an authorized administrative task needs a root-owned file:

cat <<'EOF' | sudo tee /tmp/lc-cat-root-demo.conf >/dev/null
managed=yes
EOF
sudo cat /tmp/lc-cat-root-demo.conf
managed=yes

Remove the disposable root-owned file after checking the pattern:

sudo rm -f /tmp/lc-cat-root-demo.conf

Combine Files Matched by a Glob

The shell expands chapter-*.txt before cat starts. Preview the exact order first:

printf '%s\n' chapter-*.txt
chapter-1.txt
chapter-2.txt

When the match list is correct, combine the files:

cat chapter-*.txt > chapters.txt
cat chapters.txt
chapter one
chapter two

Use exact prefixes and predictable numbering for real merges. Broad globs can accidentally include old files, backups, or a previous output file.

Reassemble Files Created by split

cat is the normal companion for rejoining chunks created by split. Create a tiny payload and split it into two ordered parts:

split -b 3 -d payload.txt payload.part-
printf '%s\n' payload.part-*
payload.part-00
payload.part-01

Previewing the expanded filenames matters because cat payload.part-* follows the shell’s sort order, not metadata from the original file:

cat payload.part-* > payload-restored.txt
cat payload-restored.txt
abcdef

For larger real files, use fixed-width numeric suffixes and an exact prefix so backup files, old chunks, or incomplete downloads do not enter the merge. The split command in Linux covers chunk sizing and suffix choices in more detail.

Use cat in Pipelines and Larger Workflows

Filter Combined Output with grep

For one file, let the search command read the file directly:

grep -F 'Install' steps.txt
Install package

Use cat when several files become one stream before filtering:

cat intro.txt steps.txt | grep -F 'service'
Verify service
Restart service

The grep command guide covers literal matching, regular expressions, recursive searches, and exit-status behavior in more depth.

Normalize a Stream with sed

cat can feed a combined stream into a text transformer when the transformer should see all inputs as one sequence:

cat intro.txt steps.txt | sed 's/service/check/'
Project notes
Install package
Verify check
Restart check

For in-place edits or safer preview patterns, use dedicated sed command examples instead of treating cat as an editor.

Avoid cat for Huge or Binary Output

cat prints everything it receives. Large files can flood the terminal, and binary files can leave control characters in the display. Prefer these tools when the task is narrower:

  • Use less file.log to page through a long text file interactively.
  • Use head -n 20 file.log for the beginning of a file.
  • Use tail -n 20 file.log or tail -f file.log for recent or live log output.
  • Use od, hexdump, or strings when binary content needs inspection.

That boundary keeps cat useful for streaming and joining, while other tools handle paging, live monitoring, and binary-safe inspection.

Use cat Safely with Edge Cases

Read a Filename That Starts with a Dash

A filename beginning with - can be parsed as an option. Add -- before the filename to stop option parsing:

cat -- -dash.txt
dash file

This pattern works across the common implementations even when their invalid-option error wording differs.

Read a File Named Exactly Dash

A filename that is exactly - collides with cat‘s standard-input marker. Prefix it with a directory component so cat receives a normal path:

cat ./-
single dash file

Check Files Without Assuming a Final Newline

cat does not add a newline between files. A file without a final newline can run into the next file’s first line:

cat -E no-newline.txt steps.txt
no newlineInstall package$
Verify service$
Restart service$

The first $ appears after Install package, proving the first file did not end before the second file started. Add a final newline to source files when clean concatenation matters.

Avoid Redirecting a File Into Itself

A command such as cat file.txt > file.txt can empty the file because the shell opens the destination for writing before cat reads the input. Write to a different destination, inspect it, and only replace the original after you know the output is correct.

cat steps.txt footer.txt > steps-with-footer.txt
cat steps-with-footer.txt
Install package
Verify service
Restart service
footer

Do Not Parse cat Output When Filenames Matter

cat prints file contents, not file names. If a workflow needs reliable filenames, use shell globs, find -print0, or a language API that keeps names separate from content. This matters for paths with spaces, tabs, newlines, leading dashes, or unusual bytes.

Troubleshoot Common cat Errors

Fix No Such File or Directory

The path does not exist from the current working directory, the name was misspelled, or the shell pattern did not match what you expected:

cat: missing.txt: No such file or directory

Confirm the current location and inspect nearby names before changing anything:

pwd
printf '%s\n' *

Use a quoted relative path or an absolute path when the file lives elsewhere. If a glob prints itself, such as *.txt, the shell did not find a match.

Fix Filenames Split by Spaces

Several No such file or directory lines can mean the shell split one filename into several arguments:

cat report final.txt
cat: report: No such file or directory
cat: final.txt: No such file or directory

Retest with the path quoted as one argument:

cat 'report final.txt'
spaced name

Fix Permission Denied

A file can exist but still block reads when your user lacks read permission:

cat: locked.txt: Permission denied

Inspect the file mode and ownership first:

ls -l locked.txt

Look for the missing r permission in the user, group, or other permission triplet that should allow the read.

If you own the file and it should be readable, restore owner read permission:

chmod u+r locked.txt
cat locked.txt

For system-owned files, avoid broad permission changes. Use sudo cat path only when you are authorized to read that file, or fix ownership and modes through the real service or account model. The chmod command in Linux covers mode changes and safer permission checks.

Fix sudo Redirection Permission Denied

A write such as sudo cat > /root/file can still fail because your current shell opens the destination before sudo runs. Use sudo tee so the privileged process owns the write:

cat <<'EOF' | sudo tee /tmp/lc-cat-root-demo.conf >/dev/null
managed=yes
EOF
sudo cat /tmp/lc-cat-root-demo.conf
managed=yes

Clean up the temporary file after the retest:

sudo rm -f /tmp/lc-cat-root-demo.conf

Fix Is a Directory

cat reads file contents, not directory entries. A directory operand produces this error:

cat: docs: Is a directory

List the directory contents with a filename-aware command, or read a real file inside it:

printf '%s\n' docs/*
cat docs/example.txt
docs/example.txt
inside docs

If the directory is empty, the glob can remain literal in Bash. Create or identify the real file before using cat.

cat Waits Without Printing Anything

A bare cat command is not frozen; it is reading from standard input. Type a line and press Enter to see it echoed, press Ctrl+D to send end-of-file, or press Ctrl+C to stop the command.

When you intended to read a file, stop the command and rerun it with the filename:

cat intro.txt

Output Looks Joined Together

If two file contents appear on the same line after concatenation, one input probably lacks a final newline. Confirm with -E:

cat -E no-newline.txt steps.txt
no newlineInstall package$
Verify service$
Restart service$

Add a newline to the source file or print a separator between inputs before combining them into a destination file.

Output Shows ^M at Line Ends

^M before each $ marker means the file uses CRLF line endings:

cat -A crlf.txt
windows^M$
next^M$

Create a Linux-line-ending copy when the consumer expects LF-only text:

tr -d '\r' < crlf.txt > lf.txt
cat -A lf.txt
windows$
next$

Keep the original file until the application, script, or import tool works with the converted copy.

Terminal Fills with Unreadable Output

Binary files and very large files are poor targets for a plain cat file. Stop the command with Ctrl+C if output is still streaming, then inspect only a small text portion with a safer tool.

head -n 5 steps.txt
tail -n 5 steps.txt

Use less for long text, tail -f for live logs, and binary-aware tools such as od, hexdump, or strings when the file is not plain text.

Clean Up the cat Example Files

Remove only the disposable directory created earlier:

cd ~
rm -rf -- ~/cat-demo

The -- marker stops option parsing before the path operand, and the target remains limited to ~/cat-demo.

Conclusion

cat is useful for short file inspection, ordered concatenation, standard-input insertion, redirection, split-file reassembly, and text diagnostics. Use -n, -b, -s, -E, -T, and -A when formatting reveals the problem, and switch to paging, search, or binary-inspection tools when the task needs more structure.

Share this guide

Help another Linux user troubleshoot faster

Share this guide with someone troubleshooting Linux systems or saving it for later.

Follow LinuxCapable

Want more LinuxCapable guides in Google?

Add LinuxCapable as a preferred source so Google can show our tutorials more often in Top Stories and mark them as preferred in AI Mode and AI Overviews when relevant.

Add LinuxCapable as a preferred source on Google
Search LinuxCapable

Need another guide?

Search LinuxCapable for package installs, commands, troubleshooting, and follow-up guides related to what you just read.

Found this guide useful?

Support LinuxCapable to keep tutorials free and up to date.

Buy me a coffeeBuy me a coffee
Before commenting, please review our Comments Policy.
Formatting tips for your comment

You can use basic HTML to format your comment. Useful tags currently allowed in published comments:

You type Result
<code>command</code> command
<strong>bold</strong> bold
<em>italic</em> italic
<a href="https://example.com">link</a> link
<blockquote>quote</blockquote> quote block

Add to the discussion

Questions, fixes, command output, and version notes help keep this guide current.

Verify before posting: