Linux

Linux cat command explained with examples

How the Linux cat command reads, joins and creates files, what its -n and -A options reveal, and when less, grep or tac do the job better, with examples.

  • 7 min read
  • 6 questions
  • +40 XP
Transcript

Cat, explained by cats. Kitty has a file of tuna orders. She wants to read it. She types cat, and the file's name. The whole file lands in her terminal. But cat is not named after cats. It's short for concatenate.

Joining things, end to end. Kitty gives it two files. It prints one, then the other. One arrow, and they're a new file. Dash n numbers every line. Dash capital A shows what's hiding. Tabs, and where every line ends.

No file at all? Cat repeats whatever Kitty types. Point that at a file, and she's writing notes with no editor. Control D, and she's done. But cat doesn't stop. A long log flies straight past her. For those, Kitty uses less.

Kitty also pipes cat into grep. Grep could have opened the file itself. Nobody tells her. And tac is cat, backwards. It prints the last line first. That's cat. It dates from 1971, 20 years before Linux. And it's the only cat that does what Kitty says.