linux · difficulty ◆
cat — concatenate files
Oldest trick in the book: dump a file, then join a few more onto it.
AI-generated draft · reviewed and editedThis text was generated with AI as a first draft, then reviewed and edited by the author, who holds editorial responsibility for it.$ catWhat it does
cat reads one or more files and writes their contents to standard output, one after another. With a single file it just prints it — which is why most people learn cat as 'print a file to the screen'. But the name is the whole story: cat is short for 'concatenate'. Its real job is joining files together, and printing one to the terminal is just the degenerate case. That dual nature is why it feels trivial and is everywhere at once.
Why it matters
cat is the atomic unit of Unix text handling. Piping cat into another tool (cat file | grep pattern) is the most common way to feed a file's contents into a pipeline. It is also how you quickly inspect a config or log without opening an editor. And because cat writes to standard output, redirecting it is how you merge or copy files without a dedicated copy tool: cat a.txt b.txt > combined.txt. Learning cat is really learning the pipe-and-redirect model that powers everything else.
Where cat goes wrong
Three habits are worth unlearning. First, `cat file | grep pattern`, the construction the Jargon File calls a useless use of cat: `grep pattern file` is shorter, and `grep pattern < file` also lets grep seek in the file instead of swallowing it as a stream. Second, `cat something > something` destroys the something — the shell truncates the output file before cat ever opens it, so a 14-byte file becomes 0 bytes with exit status 0 and no warning (verified here). There is no in-place mode and no ordering that saves you: write to a new name and move it back. Third, cat is byte-blind, which is fine for text and a liability for anything structured — concatenate two files that each begin with a UTF-8 BOM and the second BOM ends up in the middle of the stream, where the next parser has to cope with it (`cat b1.csv b2.csv | od -c` shows the three bytes reappearing mid-file). One convention that does work: a lone `-` in the argument list means standard input, so `printf 'MIDDLE\n' | cat a.txt - b.txt` prints a.txt, then the piped line, then b.txt, in that order.
Example
$ cat /etc/os-releasePRETTY_NAME="Ubuntu 24.04.4 LTS"
NAME="Ubuntu"
VERSION_ID="24.04"
VERSION="24.04.4 LTS (Noble Numbat)"
VERSION_CODENAME=noble
ID=ubuntu
ID_LIKE=debian
The simplest use: print a file straight to the terminal. This is the real head of /etc/os-release on the machine this was checked on — Ubuntu 24.04.4 — and the point survives the version bump: the values move with every point release, which is why scripts parse this file instead of hard-coding a version string anywhere.
$ cat notes1.txt notes2.txt > notes-all.txt$ cat notes1.txt notes2.txt > notes-all.txt
$ wc -l notes-all.txt
14 notes-all.txt
cat concatenates the two files into one, using > to redirect the combined output to a new file.
$ cat -n /etc/hosts 1 127.0.0.1 localhost
2 127.0.1.1 nuc
3
4 # The following lines are desirable for IPv6 capable hosts
5 ::1 ip6-localhost ip6-loopback
6 fe00::0 ip6-localnet
-n numbers every line as it prints, blank lines included, with a tab between the number and the text — handy for citing a specific line in a config or a log. This is this machine's /etc/hosts, verbatim; -b numbers only the non-blank lines, which is the one you want when the file is mostly spacing.
$ cat -A crlf.csva,b^M$
c,d^M$-A is the flag to reach for when a file looks right and parses wrong: it is -vET, so every carriage return from a Windows export shows up as ^M and every line end as $. What you see here is CRLF, the thing that turns a header into 'b\r' in a script that expects 'b' — and the reason `tr -d '\r'` exists.
Common flags
- -n
- number all output lines
- -b
- number only non-blank lines
- -A
- show all — equivalent to -vET (reveal hidden characters)
- -E
- show $ at the end of each line
- -s
- suppress repeated empty output lines
- -T
- show tab characters as ^I
- -v / --show-nonprinting
- show nonprinting characters visibly, except for tabs and end-of-line — the third letter of -A, which is exactly -vET
History
Origin
cat has been in Unix since the very first edition, released in 1971. It is one of the handful of commands that shipped on day one, and its name is simply a contraction of 'concatenate'. Over half a century later it is still the standard, and nearly every Unix and Linux system ships a version of it — on GNU systems as part of coreutils, where its current version has been maintained continuously for decades.
Fun fact
The pipe came after cat, not the other way round. cat shipped in Version 1 Unix in 1971; the pipe arrived two years later, when Ken Thompson added the pipe() system call and the `|` notation to Version 3 Unix in 1973. We say 'piping' because the mechanism is named by analogy to a physical pipeline — McIlroy proposed the idea in a 1964 memo — not because of this command; cat was simply the first tool everyone pointed at a pipe, which is why the two feel welded together. And there is a playful echo of the tool in the name of its exact opposite: tac, which is cat spelled backwards and prints files in reverse line order.
