Linux commands: cat

By

Published · Updated

Tagged: CLI

Learn how the Linux cat command prints a file to standard output, joins multiple files into one with redirection, and shows line numbers with the -n option.

Similar to tail in some way, we have cat. Except cat can also add content to a file, and this makes it super powerful.

In its simplest usage, cat prints a file’s content to the standard output:

cat file

You can print the content of multiple files:

cat file1 file2

cat prints the whole file at once. For a long file, use less instead, so you can scroll and search.

Join files

The name comes from “concatenate”. Using the output redirection operator > you can concatenate the content of multiple files into a new file:

cat file1 file2 > file3

If file3 already exists, it’s overwritten.

Using >> you can append the content of multiple files to the end of a file, creating it if it does not exist:

cat file1 file2 >> file3

Watch out for the missing newline

cat doesn’t add anything between files. If file1 doesn’t end with a newline, its last line gets glued to the first line of file2:

printf 'last' > file1
printf 'second\n' > file2
cat file1 file2
lastsecond

Most editors add the final newline for you, but files generated by scripts often don’t have it.

Never write to one of the input files

This one can hurt:

cat file1 file2 > file2   # don't do this

The shell empties file2 before cat even starts, so its content is gone.

What happens next depends on your system. GNU cat on Linux notices and stops with cat: file2: input file is output file. On macOS, cat doesn’t check: it keeps reading what it just wrote, and the file grows until you press ctrl-C or the disk fills up, and it grows fast.

If you want to add file1 to the end of file2, append instead:

cat file1 >> file2

If you want file1 before file2, write to a temporary file and rename it:

cat file1 file2 > tmp && mv tmp file2

Create a file

With no file name, cat reads from the standard input. You can use this to write a quick file from the terminal:

cat > notes.txt

Type the content, then press ctrl-D on a new line to finish.

In scripts you’ll often see it combined with a “here document”:

cat > config.txt <<'EOF'
name=$USER
port=3000
EOF

With quotes around 'EOF', the text is written as is, so config.txt contains name=$USER. Without the quotes (<<EOF), the shell expands variables first, and you’d get name=flavio.

A single dash - also means “the standard input”, so you can mix it with files:

echo "middle" | cat header.txt - footer.txt

Show line numbers and hidden characters

When watching source code files it’s great to see the line numbers, and you can have cat print them using the -n option:

cat -n file1

You can only add a number to non-blank lines using -b, or you can squeeze multiple empty lines into one using -s.

-e shows a $ at the end of each line, which is great to spot trailing spaces. It also reveals Windows line endings, which show up as ^M:

cat -e file
win^M$
line^M$

If a script fails with weird errors on a file that looks fine, this is a good first check.

The options are not all the same on Linux and macOS:

OptionWhat it doesLinux (GNU)macOS
-nnumber all lines✓✓
-bnumber non-blank lines✓✓
-ssqueeze repeated blank lines✓✓
-eshow $ at line ends, and non-printing characters✓✓
-tshow tabs as ^I, and non-printing characters✓✓
-vshow non-printing characters✓✓
-Ashow everything (same as -vET)✓—
-Eshow $ at line ends only✓—
-Tshow tabs as ^I only✓—

cat and pipes

cat is often used in combination with the pipe operator | to feed a file content as input to another command: cat file1 | anothercommand.

That works, but most commands can read the file directly. These two do the same thing:

cat app.log | grep error
grep error app.log

The second one is shorter and starts one process less. Use cat in a pipe when you want to join several files first:

cat *.log | grep error

Write to a protected file

This fails, even with sudo:

sudo cat file1 > /etc/somefile

The redirection > is done by your shell, which runs as your user, not by cat. Use tee, and run that with sudo:

cat file1 | sudo tee /etc/somefile > /dev/null

Use tee -a to append instead of overwriting.

To print the lines in reverse order, Linux has tac (cat spelled backwards). On macOS, use tail -r:

tac file      # Linux
tail -r file  # macOS

This command works on Linux, macOS, WSL, and anywhere you have a UNIX environment

Want me to talk about your product? You can sponsor this site.

Related posts about cli: