How the cat Command Works on Linux

cat, short for concatenate, is used on Linux and Unix-based systems like MacOS for reading the contents of a file, concatenating the contents with other files, and for creating new, concatenated files. It’s also frequently used to copy the contents of files.

The syntax for cat is shown below, where x is the file name, and [OPTIONS] are optional settings which alter how cat works.

shell Copycat [OPTIONS] x

Getting the Contents of a File Using cat on Linux or macOS

Using the cat command along with one file name, we can get the entire text content of a file. For example, the below command will output the content of my-file.txt into the terminal:

Similarly, we can see the contents of many files by separating them with a space. For example, the below line takes the content of my-file.txt, and my-new-file.txt, merges the content, and shows it in terminal:

cat my-file.txt my-new-file.txt

Getting the contents of files with line numbers on Linux or MacOS
We can use the option -n to show line numbers. For example, the following command merges our two files, my-file.txt, and my-new-file.txt, and outputs the content with line numbers side by side. This is pretty useful for comparing files.

cat -n my-file.txt my-new-file.txt

The output will look something like this:

1   Content from my-file.txt     1  Some more content from my-new-file.txt

Concatenating Two Files Into a New File on Linux and macOS

Since concatenate can output the contents of two files, we can use > again to merge two files into a totally new file. The below example takes my-file.txt and my-new-file.txt, merges their content, and puts it into a new file called my-combined-file.txt:

cat my-file.txt my-new-file.txt > my-combined-file.txt

Putting Content From One File Into Another With Linux or macOS

If all we want to do is put the contents of one file at the end of another, we can instead use >>. For example, the below command will take the content from my-file.txt, and put it at the end of my-new-file.txt, thus merging both files into my-new-file.txt:

cat my-file.txt >> my-new-file.txt

Line Numbers

Note: if you use >> or > with the -n option, the line numbers will also be merged into your new concatenated file!

Creating an Empty File on Linux or macOS With cat

Since it’s so easy to create files with cat, we often use it to make new files. For example, the below code will create a blank file called my-file.txt, as we are concatenating a blank string into it:

How to Show Nonprintable Characters on Linux or macOS

Some documents or files may contain nonprintable characters. These are used to signal to applications how a file should be formatted – but they can sometimes mess up the format of files. To show nonprintable characters when using cat, we can use the -v option. This will show all nonprintable characters using caret notation, so that we can view them easily.

Nonprintable Characters

Non printable characters are signals for things like character encoding. You can find a full list of nonprintable, along with their caret notation which cat uses, here.

All Options for cat on Linux or MacOS

There are a bunch of other options which help us use cat to get the ouputs we want. We’ve already discussed -n for getting line numbers, and -v for nonprintable characters, but here are the others:

  • -b – numbers only non empty output lines, overriding -n.
  • -E – displays a $ at the end of every line.
  • -s – suppresses repeated, empty lines.
  • -T – displays tabs as ^Iso as to easily discriminate them from spaces.
  • -A – equivalent to writing -vET.


Leave a Comment