cat (concatenate) prints the contents of one or more files to standard output, or of stdin if no files are given. Its original purpose was to join files together into a stream, but in practice it is most often used to dump a single file to the terminal or into a pipeline.
cat file.txt
cat file1.txt file2.txt > combined.txt
cat file.txt | grep pattern # "useless use of cat"
cat -n file.txt # with line numbers
cat -A file.txt # show tabs, line endings
The idiom cat file | grep pattern is affectionately known as the "useless use of cat", because grep pattern file does the same work more directly. Still, cat is convenient when chaining multiple pipeline stages, when making it obvious which file is the input, or when an interactive step benefits from having the file content first on the pipeline.
Beyond concatenation, cat can function as an input source (cat > file takes stdin to a file until EOF), as a quick way to create files with heredocs, or as the left end of a chain that launders line endings or character encoding. For files larger than a screenful, prefer less.
Discussed in:
- Chapter 8: Text Processing — Viewing Files
Also defined in: Textbook of Linux