cat copies bytes, nothing more
The GNU version of cat reads each named file in order and writes its bytes to standard output. It does not interpret the text. It does not know what a line is, what encoding the file uses, or whether the bytes form a valid document. For that reason it is safe to use on any file, including binary files, and it never changes the content unless you ask it to with an option. When you give no file names, or the single name -, it reads standard input instead, which is how cat is used at the end of a pipeline.
The usual reason to run cat on a single file is to see it on the screen. The usual reason to give it several files is to join them. Each file is copied in the order you name it, and nothing is added between the files, so a file that lacks a final newline will run into the start of the next one. That detail causes more surprises than any other part of cat.
cd /workspace
printf 'alpha\nbeta' > no-newline.txt
printf 'gamma\n' > tail.txt
cat no-newline.txt tail.txtThe output joins the two files onto one line, so betagamma appears where you might expect two lines. The bytes are correct, and the missing newline is the only cause. Adding a newline to the first file, or running a separate tool that normalizes the text, fixes the joined output.
Concatenation as a building block
Concatenating files is the basic operation behind many larger tasks. You can build one log from several rotated logs, one configuration from fragments, or one list of names from several lists. The command cat a b c > all creates a new file from the three inputs. The command cat a b >> c appends them to the end of an existing file. The operator > truncates the target before the command starts, so use it only when you intend to replace the file.
A common idiom reads a file and writes it somewhere else through a pipe. The command cat file | wc -l uses cat to feed standard input to wc. It is not needed for that job, because wc -l file reads the file directly, and the difference matters when the file is large because the extra process adds cost. Useful cat is the one that does the joining.
Numbering lines with cat -n
The option -n adds a line number to every line of output. The number is right-aligned in a field six characters wide, followed by a tab, and then the original line. The option -b numbers only nonblank lines. These options are handy for pointing a colleague at a specific line in a config file, and they show that cat does have a notion of lines even though it copies bytes.
The option -A makes invisible characters visible. Tabs show as ^I, the end of each line shows as a dollar sign, and carriage returns show as ^M. This is one of the fastest ways to find out whether a file has Windows line endings, which the encodings unit covers in more detail.
tac reverses the lines
The name tac is cat spelled backward. It reads its input, splits it into lines at each newline character, and writes the lines in reverse order. The last line of the file comes out first. This is useful for reading a log from the newest entry backward, or for finding the last occurrence of a value when you do not want to write a program to search.
tac must hold the whole input in memory, because it cannot print the first line until it has read the last one. For a small file that is trivial. For a multigigabyte log it is a poor choice, and tail with a line count is better. The next unit covers that tool.
cd /workspace
printf 'one\ntwo\nthree\n' | tacThe output is three, two, one, each on its own line.
Use cat -n to refer to a line in a message to someone else. Use cat -A when a file behaves oddly and you suspect hidden characters. Both options print to standard output, so you can redirect the result into a file for later.
Standard input and the dash
A program that reads standard input treats a file name as an optional convenience. The dash argument tells cat to read standard input at that point in the argument list, so cat header.txt - footer.txt wraps whatever is typed or piped between the two files. This is a clean way to add a banner to generated output. Many GNU tools accept the dash in the same way, and it is worth remembering because it lets you build command lines from pieces.
File names with spaces and exit status
File names with spaces need care. Quote the name, as in cat "my notes.txt", or escape the space with a backslash. The shell removes the quotes before cat starts, so cat receives one argument that contains a space. Without the quotes, cat receives two arguments and reports that the first one does not exist. That message is confusing when the file plainly exists, and it is the first thing to check when a name seems to be missing.
In scripts, build lists of names with a glob or with find and a null delimiter, and pass them as separate quoted arguments. Never build a command line by pasting names into a string and parsing it again. The shell sorts glob matches according to the locale, so the order cat prints may not match the order you expect when names mix uppercase and lowercase letters or punctuation.
The exit status of cat is zero when every file was copied. If any file cannot be read, cat reports the error on standard error, keeps going, and exits with a nonzero status at the end. Running cat missing.txt existing.txt prints the contents of the second file and shows the error for the first. A script that depends on complete output must test the status, not only the text on the screen.
Practice in the lab
The lab creates two small files and asks you to join them, reverse the joined result, and number the lines. Each output goes to a file in /workspace, and the checker compares the bytes exactly. Watch the newline at the end of each file, because the comparison is strict about it.