| Tested on | Ubuntu 25.04 (Plucky Puffin) |
|---|---|
| Package | uutils-coreutilscat (uutils coreutils) 0.2.2 |
| Applies to | Ubuntu, Debian, RHEL, Rocky Linux, AlmaLinux, Oracle Linux, CentOS Stream, Fedora, Arch Linux, SUSE, openSUSE, Alpine |
| Privilege | read-only (no elevated privileges) |
| Man page | cat(1) |
| Scope | cat reads files or standard input and writes the bytes to standard output. Use it to view small text files, join files in order, or pipe stdin into a new file with shell redirection. |
| Related guides | Linux commands |
cat — quick reference
Viewing and concatenating
Print file contents to the terminal or join several files in order.
| When to use | Command |
|---|---|
| Print one or more files to the terminal | cat file |
| Print several files as one stream (order preserved) | cat file1 file2 |
| Read from standard input (same as omitting files) | cat - |
| Create or overwrite a file from typed input (Ctrl+D ends input) | cat > newfile |
Line numbers and blank lines
Number lines or collapse repeated empty lines when comparing configs or logs.
| When to use | Command |
|---|---|
| Number every output line | cat -n file |
| Number only non-empty lines | cat -b file |
| Squeeze repeated blank lines into one | cat -s file |
Invisible characters
Expose tabs, trailing spaces, and line endings when a file looks correct but diff or scripts fail.
| When to use | Command |
|---|---|
Show $ at the end of each line |
cat -E file |
Show TAB characters as ^I |
cat -T file |
Show ends, tabs, and non-printing chars (-A = -vET) |
cat -A file |
Same as -A using the long option |
cat --show-all file |
Help and version
| When to use | Command |
|---|---|
| Show built-in usage text | cat --help |
| Print the installed package version | cat --version |
cat — command syntax
cat concatenates each FILE to standard output. Synopsis from cat --help on Ubuntu 25.04 (cat uutils coreutils 0.2.2):
cat [OPTION]... [FILE]...With no FILE, or when FILE is -, cat reads standard input. It does not edit files in place — use shell redirection (>, >>) or tee when you need to save output.
cat — command examples
Essential View a small text file
Use cat when you want the whole file on screen and it is short enough to read without a pager.
Run the command:
WORKDIR=/tmp/cslab
mkdir -p "$WORKDIR" && cd "$WORKDIR"
printf 'line one\nline two\n\nline four' > sample.txt
cat sample.txtSample output:
line one
line two
line fourFor multi-megabyte logs, prefer less so the terminal does not flood.
Essential Concatenate two files in order
Join files when you need one continuous stream — for example merging chapter files before archiving.
Run the command:
WORKDIR=/tmp/cslab
mkdir -p "$WORKDIR" && cd "$WORKDIR"
printf 'alpha\n' > a.txt
printf 'beta\n' > b.txt
cat a.txt b.txtSample output:
alpha
betaThe second file always follows the first; order matters when building scripts or reports.
Essential Create a file from standard input
cat with > captures typed lines or piped data into a new file.
Run the command:
WORKDIR=/tmp/cslab
mkdir -p "$WORKDIR" && cd "$WORKDIR"
printf 'hello\n' | cat - > created.txt
cat created.txtSample output:
helloPress Ctrl+D after typing at an interactive prompt to close stdin. Use >> with cat when you want to append instead of overwrite.
Common Number every line with -n
Line numbers help when you discuss a config file with a teammate or match grep output back to a file.
Run the command:
WORKDIR=/tmp/cslab
mkdir -p "$WORKDIR" && cd "$WORKDIR"
printf 'line one\nline two\n\nline four' > sample.txt
cat -n sample.txtSample output:
1 line one
2 line two
3
4 line fourTabs between the number and text are normal on this build.
Common Collapse repeated empty lines with -s
Log files sometimes contain runs of blank lines. -s prints at most one consecutive empty line.
Run the command:
WORKDIR=/tmp/cslab
mkdir -p "$WORKDIR" && cd "$WORKDIR"
printf 'line one\nline two\n\nline four' > sample.txt
cat -s sample.txtSample output:
line one
line two
line fourPair with -n only when you need both features; -b overrides -n for numbering rules.
Common Reveal line endings and TAB characters
When diff reports a mismatch but the lines look identical, invisible characters are often the cause.
Run the command:
WORKDIR=/tmp/cslab
mkdir -p "$WORKDIR" && cd "$WORKDIR"
printf 'tab\there\n' > tabs.txt
cat -E tabs.txt
cat -T tabs.txtSample output:
tab^Ihere$
tab^Ihere-E adds $ at line ends; -T exposes TAB as ^I without showing other non-printing characters.
Advanced Debug trailing spaces with -A
-A combines -vET — useful when a line has trailing spaces that break parsers.
Run the command:
WORKDIR=/tmp/cslab
mkdir -p "$WORKDIR" && cd "$WORKDIR"
printf 'trail ' > trail.txt
cat -A trail.txtSample output:
trail $The space before $ is the trailing whitespace you were hunting.
Advanced Number only non-empty lines with -b
Use -b when blank separator lines should stay unnumbered but data lines need indexes.
Run the command:
WORKDIR=/tmp/cslab
mkdir -p "$WORKDIR" && cd "$WORKDIR"
printf 'line one\nline two\n\nline four' > sample.txt
cat -b sample.txtSample output:
1 line one
2 line two
3 line fourThe empty line stays blank; only lines with text receive numbers.
cat — when to use / when not
| Use cat when | Use something else when |
|---|---|
| You need to view or join small text files quickly | The file is huge — use less or head / tail |
You are creating or appending a short file from stdin with > or >> |
You need an editor — cat does not modify lines in place |
You must inspect invisible characters (-A, -E, -T) |
You only need a pattern match — grep reads files directly |
| You are concatenating a fixed list of files in order | You would write cat file | grep — run grep pattern file instead |
cat vs less
| cat | less | |
|---|---|---|
| Output | Dumps the entire file to the terminal | Pages through file interactively |
| Best for | Small files, concat, debugging whitespace | Logs, man pages, large configs |
| Scrollback | Terminal history only | Built-in search and navigation |
Use less when output would scroll off screen.
cat — interview corner
What does the cat command do in Linux?
cat (concatenate) reads one or more files — or standard input — and writes the combined bytes to standard output. It does not change the source files unless you redirect stdout to a path.
Typical jobs:
- Print a short config or script to the terminal
- Join
part1andpart2into one stream - Feed stdin into a new file:
cat > outfile
On Ubuntu 25.04, cat comes from uutils coreutils; flags match the traditional POSIX/GNU set (-n, -A, -E, …).
A strong answer is:
"cat sends file contents or stdin to stdout — view small files, concatenate them in order, or capture typed input with redirection. It does not edit files in place."
Why do people say 'useless cat' in shell pipelines?
In a pipeline like cat file | grep pattern, grep already opens file — the extra cat adds a process and a pipe with no benefit. The same applies to awk and sed: pass the filename as their last argument.
Good uses of cat are not about feeding filters — they are concatenation, stdin capture, and showing invisible characters with -A / -E / -T.
A strong answer is:
"cat file | grep is redundant because grep reads files directly. I use cat for concat, stdin redirects, or debugging whitespace — not as a universal pipe prefix."
What is the difference between cat -n and cat -b?
Both add line numbers to output, but they treat blank lines differently:
| Flag | Blank lines |
|---|---|
-n |
Numbered like any other line |
-b |
Skipped — only non-empty lines get numbers |
On Ubuntu 25.04, -b overrides -n when both are given.
A strong answer is:
"cat -n numbers every line including blanks; cat -b numbers only non-empty lines, which is handy when blank separators should stay unnumbered."
How do you show hidden characters in a file with cat?
Use visibility flags from the quick-reference deck:
| Flag | Shows |
|---|---|
-E |
$ at end of each line |
-T |
TAB as ^I |
-A |
Combination of -vET — ends, tabs, and other non-printing notation |
Example for trailing spaces:
cat -A suspicious.confA strong answer is:
"I use cat -A for full visibility, or cat -E / cat -T when I only care about line endings or tabs — faster than guessing why diff disagrees."
What does cat - mean?
A lone - tells cat to read standard input instead of a path. It is equivalent to omitting filenames when stdin is already connected — common in somecommand | cat - > outfile so the same redirection pattern works in scripts.
A strong answer is:
"cat - reads stdin; I use it in pipelines when I want explicit stdin handling before redirecting to a file."
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Terminal floods with text | File is very large | Use less file or head / tail |
cat: file: No such file or directory |
Wrong path or typo | Check with ls -l or tab-complete |
Permission denied |
File not readable by your user | ls -l file; use sudo only when appropriate |
| Output looks empty but file has size | Binary file or NUL bytes | Use file, hexdump, or less |
Unknown option |
uutils vs GNU flag mismatch | Run cat --help on your host |

