TExcelTAD
Home › Linux & Bash › cat: Show Files

cat: Show Files

cat (concatenate) reads files and writes their contents to the screen, one after another. It's the quickest way to look at a short file, join several files and, with redirection, create small ones.

Showing a file

bash · try it yourself
printf 'host=db.internal\nport=5432\nuser=app\n' > db.conf
cat db.conf
host=db.internal
port=5432
user=app

Joining files

Give cat several files and it outputs them in order. With > you save the combined result: that is where the name concatenate comes from.

bash · try it yourself
echo "== January ==" > jan.txt
echo "rent 900" >> jan.txt
echo "== February ==" > feb.txt
echo "rent 900" >> feb.txt
echo "car 240" >> feb.txt
cat jan.txt feb.txt > q1.txt
cat q1.txt
== January ==
rent 900
== February ==
rent 900
car 240

Warning: Never redirect into one of the input files: cat a.txt b.txt > a.txt first empties a.txt (the shell truncates it before cat starts), and its content is lost. GNU cat detects the obvious case and complains: input file is output file.

Useful options

Option Effect
-n Number all lines
-b Number only non-empty lines
-s Squeeze several blank lines into one
-A Show invisible characters: $ at line ends, ^I for tabs, ^M for Windows carriage returns
bash · try it yourself
printf 'first\n\n\n\nsecond\nthird\n' > notes.txt
cat -n notes.txt
echo "---"
cat -sb notes.txt
     1	first
     2
     3
     4
     5	second
     6	third
---
     1	first

     2	second
     3	third

-A is a debugging superpower. A script that fails with strange errors like

#39;\r': command not found was saved with Windows line endings, and cat -A shows them:

bash · try it yourself
printf 'name\tage\r\nAda\t36\r\n' > people.tsv
cat -A people.tsv
name^Iage^M$
Ada^I36^M$

Each ^M$ reveals a carriage return before the newline; dos2unix or sed -i 's/\r$//' file removes them.

Creating a file with cat and a here-document

cat > file copies whatever it reads to the file. Interactively you'd type the text and finish with Ctrl+D. In scripts you feed it a here-document, a block of text that ends at a marker word:

bash · try it yourself
cat > todo.md <<'EOF2'
# Today
- review pull request
- update the README
EOF2
cat todo.md
wc -l todo.md
# Today
- review pull request
- update the README
3 todo.md

cat and pipes

cat also reads its input when it has no file arguments (or the argument -), so it can sit in a pipeline. But you rarely need it there: most tools read files directly. cat file | grep x works, but grep x file is shorter and avoids an extra process.

bash · try it yourself
printf 'apple\nbanana\napricot\n' > fruit.txt
cat fruit.txt | grep ap
grep ap fruit.txt
echo "header line" | cat - fruit.txt
apple
apricot
apple
apricot
header line
apple
banana
apricot

cat - fruit.txt prints what arrives through the pipe (-) first, then the file.

Related commands

Command Use it for
less file Scrolling through long files (q quits, /word searches)
head / tail The first or last lines
tac Print the file with the lines reversed (cat backwards)
nl Number lines with more options than cat -n
bash · try it yourself
printf 'step 1\nstep 2\nstep 3\n' > steps.txt
tac steps.txt
step 3
step 2
step 1

Test yourself

What does cat a.txt b.txt > all.txt do?

  1. Copies a.txt into b.txt
  2. Writes the contents of a.txt followed by b.txt into all.txt
  3. Compares the two files
  4. Appends all.txt to a.txt

Which option makes Windows line endings visible as ^M?

  1. -n
  2. -s
  3. -A
  4. -b

Which is the simplest way to search a file for error?

  1. cat log | grep error
  2. grep error log
  3. cat grep error log
  4. error < grep log