ZetCode

Linux cat Command

last modified October 23, 2026

The cat (concatenate) command in Linux reads files in sequence and writes their contents to standard output. It is used to display short files, join several files into one, create small files from the keyboard, and feed files into pipelines. With options, it also numbers lines and makes invisible characters visible.

For long files, a pager such as less is more practical, and for the first or last lines, head and tail are better suited. The examples use GNU cat from coreutils.

Displaying a file

The basic use of cat prints a file to the terminal.

cat animals.txt

The file content is written to standard output unchanged.

$ cat animals.txt
An old falcon
A red fox

Concatenating files

Given several files, cat prints them one after another. With redirection, the result is saved to a new file.

cat animals.txt nature.txt
cat animals.txt nature.txt > all.txt

This is where the command gets its name. The files are joined in the order given on the command line. The same technique joins split archives, for example cat backup.part* > backup.tar.

$ cat animals.txt nature.txt
An old falcon
A red fox
A blue sky
A green forest
$ cat animals.txt nature.txt > all.txt
$ cat all.txt
An old falcon
A red fox
A blue sky
A green forest

Numbering lines

The -n option numbers all output lines, and -b numbers only non-empty lines.

cat -n spaced.txt
cat -b spaced.txt

Line numbers help when referring to specific lines, for example in error messages. With -b, empty lines are printed without a number.

$ cat -n spaced.txt
     1	first paragraph
     2
     3
     4
     5	second paragraph
     6
     7	third paragraph
$ cat -b spaced.txt
     1	first paragraph



     2	second paragraph

     3	third paragraph

Squeezing empty lines

The -s option replaces runs of empty lines with a single empty line.

cat -s spaced.txt

The three empty lines after the first paragraph are reduced to one, which tidies up text with irregular spacing.

$ cat -s spaced.txt
first paragraph

second paragraph

third paragraph

Showing invisible characters

The -A option makes tabs, line ends, and other non-printing characters visible.

cat data.csv
cat -A data.csv
cat -ET data.csv

In plain output, the file looks normal. With -A, tabs are shown as ^I, the end of each line as $, and carriage returns as ^M, which reveals Windows line endings and the trailing space on the last line. -A is equivalent to -vET: -v shows non-printing characters, -E marks line ends, and -T shows tabs.

$ cat data.csv
name	city
Paul	London
trailing space
$ cat -A data.csv
name^Icity^M$
Paul^ILondon^M$
trailing space ^M$
$ cat -ET data.csv
name^Icity^M$
Paul^ILondon^M$
trailing space ^M$

Creating a file

Without a file argument, cat reads standard input. Redirecting its output creates a file from what we type.

cat > todo.txt

We type the lines and press Ctrl+D at the beginning of an empty line to signal the end of input. The > operator overwrites the file if it already exists.

$ cat > todo.txt
buy milk
call Peter
$ cat todo.txt
buy milk
call Peter

Appending to a file

The >> operator appends the typed text to an existing file.

cat >> todo.txt

The new line is added to the end of todo.txt; the existing content is kept.

$ cat >> todo.txt
water plants
$ cat todo.txt
buy milk
call Peter
water plants

Writing files with a here document

In scripts, cat is combined with a here document to write multi-line files.

config.sh
#!/bin/bash

cat > config.ini << EOF
[server]
host = localhost
port = 8080
EOF

The lines between << EOF and the closing EOF become the standard input of cat, which writes them to config.ini. Variables in the text are expanded unless the delimiter is quoted as 'EOF'. See redirection and pipes for details.

$ ./config.sh
$ cat config.ini
[server]
host = localhost
port = 8080

Reading standard input among files

A dash in the list of files stands for standard input.

echo "--- header ---" | cat - animals.txt

The text from the pipe is inserted before the contents of the file. The dash can be placed anywhere in the list, so it is also possible to add a footer.

$ echo "--- header ---" | cat - animals.txt
--- header ---
An old falcon
A red fox

cat in pipelines

cat is often used at the start of a pipeline, although many commands can read files directly.

cat app.log | grep error
grep error app.log
tac animals.txt

Both of the first two commands produce the same result. Passing the file directly to grep saves a process; using cat in front of a command that accepts file names is known as a useless use of cat. The related tac command prints a file with its lines in reverse order.

$ cat app.log | grep error
error: disk full
error: timeout
$ grep error app.log
error: disk full
error: timeout
$ tac animals.txt
A red fox
An old falcon

Best practices for cat

Source

GNU cat manual

In this article, we have used the cat command to display, concatenate, create, and append files, number lines, squeeze empty lines, and reveal invisible characters.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Linux tutorials.