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
| Option | What it does |
|---|---|
-n | number all lines, including blank ones |
-b | number only non-blank lines (overrides -n) |
-s | squeeze 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
| Option | What it does |
|---|---|
-E | show the end of a line as $: makes trailing spaces visible |
-T | show a tab as ^I |
-v | show control characters in ^X notation (\r → ^M, \x01 → ^A), and bytes above 127 as M-…; leaves newline and tab alone |
-A | everything at once: same as -vET |
-e | same as -vE |
-t | same as -vT |
-u | ignored, kept for compatibility |
tac options
| Option | What it does |
|---|---|
-s string | "line" separator — this string instead of a newline |
-r | treat the separator from -s as a regular expression |
-b | put 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 > asilently losesa. The shell truncatesabeforecatstarts, so the file ends up containing onlyb, with no error. Write to a new file and rename it afterwards.cat f >> fdoesn't loop forever; it fails withinput file is output fileand exit code 1.- A last line without a newline gets glued to the next file. If
aends withbwithout\n,cat a coutputsbcas one line. Check withtail -c1 file | xxd. tacalso "glues" such a line: for input1\n2without a final\nyou get21\n.-vin theClocale (as in the sandbox) shows accented letters as garbage likeM-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\rand tabs.<<-strips only tabs. If your editor replaced them with spaces, the indent stays, and a closingEOFwith spaces isn't recognized at all — the here-doc "eats" the rest of the script.- An extra
catin a pipeline ("useless use of cat"):cat file | grep xworks but starts an extra process.grep x fileorgrep x < filedo 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
catbinary files to the terminal: control bytes can mess up the terminal settings. View them with xxd and od orcat -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