Printing and joining files with cat
- Version
- unversioned
- Review
- Draft
Summary#
cat writes the contents of one or more files to standard output in the order given.
Syntax#
cat notes.txt
cat header.txt body.txt > page.txt
cat -n notes.txt
cat -A notes.txt
cat > config.ini <<'END'
port=8080
END
-n numbers lines and -A makes tabs, line ends and control characters visible. A here-document passes the lines up to the marker as input; quoting the marker keeps $ unchanged.
Example#
(
set -eu
dir=$(mktemp -d)
trap 'rm -rf -- "$dir"' EXIT
cd "$dir"
cat > first <<'END'
one $HOME
END
printf 'two\t2\r\n' > second
cat first second > both
cat -A both
)
Expected output#
-A shows the tab as ^I, the carriage return as ^M and each line end as $.
one $HOME$
two^I2^M$
Common mistakes#
> truncates the output file before cat runs, so cat a b > a destroys the old content of a. Use a temporary file, then move it into place. Printing a binary file can corrupt the terminal state; use file, xxd or less. A command such as cat file | grep text works but grep text file avoids the extra process and keeps the file name. ^M in the output means the file has Windows line endings.
Version applicability#
This entry describes cat from GNU Coreutils. The -A option is not available in BSD and macOS cat.
Sources#
- Attribution
- DevLookup contributors
- License
- CC-BY-4.0