Home·Terminal commands

cat

Files and directories

Usage

cat [file...]

Prints files one after another; it dumps everything at once, so long files want less and cat file | grep is one pipe too many.

Common flags

FlagMeaning
-nNumber every line
-bNumber only the non-blank lines
-sSqueeze runs of blank lines into one
-vShow non-printing characters
-eBSD: -v plus a $ at each line end; GNU spells this -A

Examples

cat a.txt b.txt > all.txt

Joins two files into one.

cat -n script.sh

Prints it with line numbers.

cat > note.txt

Types a new file from the keyboard until Ctrl-D.

Moving, deleting and finding. Some of these do not ask twice, which is why -i becomes a habit.

How to read this

  • Square brackets [ ] mark a part you may leave out.
  • An ellipsis … means you can list more than one.
  • Flags are case-sensitive — in some commands -r and -R do different things.

Questions

Q. What does cat do?

Prints files one after another; it dumps everything at once, so long files want less and cat file | grep is one pipe too many.

Q. How do I type it?

cat [file...] — square brackets mark the parts you can leave out.

Q. How many flags are worth knowing?

5 are listed here; the full set is in man cat. This command sits under Files and directories.

Related commands

man page: man cat