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
printf 'host=db.internal\nport=5432\nuser=app\n' > db.conf
cat db.confhost=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.
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.txtfirst emptiesa.txt(the shell truncates it beforecatstarts), and its content is lost. GNUcatdetects 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 |
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:
printf 'name\tage\r\nAda\t36\r\n' > people.tsv
cat -A people.tsvname^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:
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.
printf 'apple\nbanana\napricot\n' > fruit.txt
cat fruit.txt | grep ap
grep ap fruit.txt
echo "header line" | cat - fruit.txtapple
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 |
printf 'step 1\nstep 2\nstep 3\n' > steps.txt
tac steps.txtstep 3
step 2
step 1
Test yourself
What does cat a.txt b.txt > all.txt do?
- Copies a.txt into b.txt
- Writes the contents of a.txt followed by b.txt into all.txt
- Compares the two files
- Appends all.txt to a.txt
Which option makes Windows line endings visible as ^M?
- -n
- -s
- -A
- -b
Which is the simplest way to search a file for error?
- cat log | grep error
- grep error log
- cat grep error log
- error < grep log