Conquer The Command Line: The cat Command

Jim Homme
2 min readJan 6, 2021

In this installment of “Conquer The Command Line” we talk about how to use the Linux cat command.

Whenever you want to display or paste files end-to-end, type cat. cat will help you manipulate files.

Memory Aid: Think “chain”

Just as chains have links that make them longer, programmers often paste content to the end of other content. The term for this is “concatenation.” You can think of cat as short for concatenation.

Displaying Files

In its simplest form, cat can display one or more files on the screen. It displays the whole file.

To display one file, type cat file where file is any file name including the path. If you type more than one file name after the cat keyword, cat retrieves the files in the order you type them and displays them on the screen.

Making a Chain Of Files

To use cat to paste files together to make bigger files, you have to use cat with some command line tricks .

Remember what I said about a chain? You can use cat to paste two files together and create a third like this.

cat file1 file2 > file3

This command wipes out the file if it exists.

You can also use cat to append, put more content at the end of, a file if it already exists like this.

cat file1 file2 >> file3

This makes a new file if it didn’t already exist and keeps the file in tact if it exists.

When you use > and >> to do the above tricks, it's called "redirection. It's kind of like posting a detour for traffic when a construction project is underway. See this page on redirection.

Essential Options

Cat has some command line options you might want to use.

To display line numbers along with a file, type cat -n file1.

To display line numbers only on non-blank lines, type cat -b file.

To display a file and remove all blank lines, type cat -s file.

To display non-printing characters, type cat -V file.

To make cat display $ at the ends of lines, type cat -E.

Remember you can always use multiple options on any Linux command.

cat is often used in combination with the pipe operator | to feed a file content as input to another command like this. cat file1 | anothercommand.

Hopefully this post has helped you take one more step in conquering the command line. I welcome any feedback.

This post was originally written on https://www.jimhomme.com

--

--

Jim Homme

Musician, father of three, husband. Information Technology veteran. Bookworm. Chess player. Accessibility consultant. Plain language advocate