Use cat to Inspect, Join and Stream Text Safely

cat looks trivial until a mystery file with no trailing newline joins onto the next one and quietly corrupts your output. This is a small, repeatable workflow for using GNU cat to display files, concatenate inputs in a chosen order, pass standard input through a pipeline, and inspect awkward whitespace. The examples target GNU coreutils 9.4, the version installed on this machine.

Allow about ten minutes. You need a shell and one or more readable files. The normal examples are unprivileged: do not use sudo just to read a file, only when the file permissions genuinely require it and your system policy allows that access.

1. Confirm the installed command

Check which executable your shell will run, then ask it for its version. Both only read local command metadata:

$ command -v cat
/usr/bin/cat
$ cat --version | head -1
cat (GNU coreutils) 9.4

The exact path can differ between systems. The useful checkpoint is knowing it is GNU cat and knowing its version, for whenever you are writing a script or chasing a difference between machines.

2. Display one file

Give the file name as an argument. This writes the file's contents to standard output and does not alter the file:

$ cat /path/to/notes.txt

For a safe first test, create a temporary input somewhere you control, then read it:

$ printf 'first line\nsecond line\n' > /tmp/cat-example.txt
$ cat /tmp/cat-example.txt
first line
second line

The shell's > does the file creation here, not cat. If /tmp/cat-example.txt already exists, that redirection truncates it before printf even runs, so use a new name or check the destination first when the contents matter.

Checkpoint: Verify the source still exists and has the expected size:

$ test -s /tmp/cat-example.txt && echo readable-and-non-empty
readable-and-non-empty

3. Concatenate files in order

Pass several file names. GNU cat emits the first file, then the second, with no separator added between them:

$ printf 'alpha\n' > /tmp/cat-a.txt
$ printf 'beta\n' > /tmp/cat-b.txt
$ cat /tmp/cat-a.txt /tmp/cat-b.txt
alpha
beta

Order is part of the command: reverse the arguments and you reverse the output. If the first file has no final newline, the next file starts on the same output line. That is faithful to the actual bytes, so never assume separate arguments create record boundaries.

A common use is producing a combined file, so protect the existing destination. Plain redirection truncates it before cat can even report an input error:

$ cat /tmp/cat-a.txt /tmp/cat-b.txt > /tmp/cat-combined.new
$ test -s /tmp/cat-combined.new && mv /tmp/cat-combined.new /tmp/cat-combined.txt
$ cat /tmp/cat-combined.txt
alpha
beta

This writes the new output first and only replaces the old name once the command succeeds and the new file is non-empty. If the command fails, inspect /tmp/cat-combined.new before removing it. Do not use this pattern blindly where an empty result is actually valid: the size check is an example policy, not a guarantee for every file format.

4. Read standard input

With no file arguments, cat reads standard input. Type a line and press Control-D on an empty line to send end-of-file in an interactive terminal:

$ cat
type a line
type a line
^D

The two displayed lines are the same input, echoed by the terminal and then written by cat; ^D is a terminal indication, not command output. A more reproducible test pipes data straight in:

$ printf 'from standard input\n' | cat
from standard input

A single hyphen is also the documented standard-input marker, which lets you put input between named files:

$ printf 'middle\n' | cat /tmp/cat-a.txt - /tmp/cat-b.txt
alpha
middle
beta

Do not use a hyphen when you mean a file literally named - unless you have checked how that program handles it. For cat, it always means standard input.

5. Make invisible text easier to inspect

Use the display options when a file appears to have missing, repeated or oddly spaced lines:

For a compact diagnostic, combine -n and -E:

$ printf 'one\n\ntwo' | cat -nE
     1  one$
     2  $
     3  two

The final line has no newline, so it gets no trailing dollar sign. That single distinction often explains why a generated file looks wrong in another tool. -A is shorthand for -vET, -e for -vE, and -t for -vT.

6. Avoid the common traps

cat is a byte-stream tool, not a parser: it does not add separators, interpret line endings, sort records or validate a file format. Keep that in mind before feeding its output to another command. For large files, dumping everything can flood a terminal, so pipe to a pager or a more targeted inspection command instead.

Warning: Never edit a file in place with a command such as cat file > file. The shell truncates file before cat ever reads it, so the original contents are simply gone. Recovery needs a separate backup or another copy of the data; there is no cat undo.

When an input cannot be opened, the command reports an error and normally carries on to later operands. Check the exit status in a script, and make failures visible:

$ cat /path/to/missing.txt /tmp/cat-a.txt
cat: /path/to/missing.txt: No such file or directory
alpha
$ printf 'status: %s\n' "$?"
status: 1

The wording varies with locale and the missing path. What matters is that the expected input was readable and the status was zero.

Done means