← Full reference

cat and tac

Print a file, join several files, number lines, show invisible characters

cat (from concatenate) reads files one after another and outputs their contents back to back. It's used to view short files, join several files into one, create a file from a here-doc and check a text for tabs, \r and trailing spaces. tac does the same but prints the lines in reverse order — from the last to the first.

Syntax

cat [options] [file…]
tac [options] [file…]

Without files, or with - instead of a name, both commands read standard input. cat a - b outputs a, then standard input, then b.

cat options

Numbering and blank lines

OptionWhat it does
-nnumber all lines, including blank ones
-bnumber only non-blank lines (overrides -n)
-ssqueeze several consecutive blank lines into one

The number is right-aligned in a 6-character field, followed by a tab. With several files the numbering is continuous: it doesn't restart for each file.

Invisible characters

OptionWhat it does
-Eshow the end of a line as $: makes trailing spaces visible
-Tshow a tab as ^I
-vshow control characters in ^X notation (\r → ^M, \x01 → ^A), and bytes above 127 as M-…; leaves newline and tab alone
-Aeverything at once: same as -vET
-esame as -vE
-tsame as -vT
-uignored, kept for compatibility

tac options

OptionWhat it does
-s string"line" separator — this string instead of a newline
-rtreat the separator from -s as a regular expression
-bput the separator before a record instead of after

Here-doc: create a file from text

# variables and $(…) inside are expanded
cat <<EOF > app.conf
host=$HOSTNAME
port=8080
EOF

# quoted delimiter word: the text is written as is, no substitution
cat <<'EOF' > script.sh
echo "$HOME"
EOF

# <<- strips leading tabs (not spaces!) — handy inside if blocks and functions
cat <<-EOF
	the tab indent will disappear
	EOF

# append to the end of the file instead of overwriting
cat <<EOF >> app.conf
debug=false
EOF

Examples

printf 'one\n\n\n\ttwo  \nthree\r\n' > sample.txt

# just print the file
cat sample.txt

# join files into one (in argument order)
printf 'a\n' > part1; printf 'b\n' > part2
cat part1 part2 > all.txt

# line numbers: all / non-blank only
cat -n sample.txt
cat -b sample.txt

# remove extra blank lines
cat -s sample.txt

# see tabs (^I), Windows \r (^M) and spaces before $
cat -A sample.txt

# insert standard input between two files
echo '---' | cat part1 - part2

# the last log lines first
printf '10:00 start\n10:05 work\n10:10 stop\n' | tac

# tac with a different separator
printf 'a,b,c,' | tac -s ,

Pitfalls

  • cat a b > a silently loses a. The shell truncates a before cat starts, so the file ends up containing only b, with no error. Write to a new file and rename it afterwards.
  • cat f >> f doesn't loop forever; it fails with input file is output file and exit code 1.
  • A last line without a newline gets glued to the next file. If a ends with b without \n, cat a c outputs bc as one line. Check with tail -c1 file | xxd.
  • tac also "glues" such a line: for input 1\n2 without a final \n you get 21\n.
  • -v in the C locale (as in the sandbox) shows accented letters as garbage like M-CM-) for é, because every UTF-8 byte of them is above 127. That doesn't get in the way if you're only looking for \r and tabs.
  • <<- strips only tabs. If your editor replaced them with spaces, the indent stays, and a closing EOF with spaces isn't recognized at all — the here-doc "eats" the rest of the script.
  • An extra cat in a pipeline ("useless use of cat"): cat file | grep x works but starts an extra process. grep x file or grep x < file do the same. It's not an error, just a habit worth knowing about; besides, a command given the file as an argument can print its name and know its size.
  • Don't cat binary files to the terminal: control bytes can mess up the terminal settings. View them with xxd and od or cat -v.
  • A big file dumped to the terminal is inconvenient. To view the beginning or end use head and tail; to page through it — less.

See also

head and tail · nl · tee · redirection · xxd and od · rev