Skip to content

Instantly share code, notes, and snippets.

@jhomme

jhomme/blog.md Secret

Created January 6, 2021 11:51
Show Gist options
  • Save jhomme/adbdf59e40b14b001d0c83b44ffd5e8b to your computer and use it in GitHub Desktop.
Save jhomme/adbdf59e40b14b001d0c83b44ffd5e8b to your computer and use it in GitHub Desktop.

How to use the Linux cat command plus essential options

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

Memory Aid: Think "chain"

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

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

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. To use cat to paste files together to make bigger files, you have to use cat with other commands or employ some command line tricks to accomplish these tasks.

Pasting Two Files Together To Make A Third File

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.

What Now?

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment