What Is the CAT Command in Linux and How to Use It

Cat Command Linux Featured

The cat command is Linux’s most basic way of combining files together. It’s short for conCATenation, but there’s more to this than just copy-pasting one file next to the other.

Basic Format for the CAT Command

This is how you normally use the cat command:

cat file1 file2 file3 > output

The order for appending files next to each other is based on the order you wrote the files within the command. In this example, when you open up the “output” file, the contents of “file1” will be followed by “file2” then “file3” and so on.

Linux Terminal Combining File1 File2 And File3 Output To Output File With Output Contents On Text File Editor

You can add as many files as you like as long as your filesystem can support the output’s eventual size.

Also, like in all Linux terminal commands, you will need to include the file extension when you add them in. For example, if you were working with JSON files and planning to combine two of these together, you’ll need to type it like this:

cat file1.json file2.json > combined_file.json

CAT without the Output Portion

For cat, the output part is optional. Removing this part prints out the contents of each file into the terminal.

Linux Terminal Cat Command Cat File1 Txt File2 Txt

You can also do this on single files. Do remember that you won’t be able to edit the content here like you would with DOC or TXT files on a text editor. The cat command becomes more like a text file viewer than an editor.

CAT Command Flags

There are six flags you can use whenever you use CAT on Linux:

  • -n enumerate every line starting from 1.
  • -b enumerate only the lines that contain characters.
  • -s squeeze multiple consecutive empty lines into one empty line.
  • -e mark the end of each line.
  • -q do not display a message if CAT can not find an input file.
  • -t display tab characters as ^I.

We’ll go through how each one works and how you can use them.

Using the -n Flag

The -n flag enumerates the contents of a file per line and prints it on the terminal. It does not edit the file itself.

cat -n file1.txt

But you can still save the output (with enumeration) by doing this:

cat -n file1.txt > file2.txt
Linux Terminal Cat Command Flags N Flag

That enumerates the content of “file1.txt” and saves it to “file2.txt”.

You can also enumerate multiple files together. The cat command will continue counting after the end of the first file. If the first file ends with 33, the next file will start at 34 and so on.

Tip: You can also make use of diff command to find the difference between files first before combining them together.

Using the -b Flag

The problem with the -n flag is that it also enumerates empty lines. If you had a file with plenty of whitespace for layout or readability, you’ll need the -b flag instead.

cat -b file1.txt
Linux Terminal Cat Command Flags B Flag 2

The format for the -b flag works the same as that of the -n flag. Also, if you use the two of these together, the -b flag will override the -n flag no matter how you order them.

Using the -s Flag

The -s flag is another handy cat command flag for when you have plenty of white space in your files. It works by reducing all repeating whitespace into a single one, which eases readability.

cat -s file2.txt
Linux Terminal Cat Command Flags S Flag

This flag looks for at least 2 consecutive empty lines before merging them. If your file contains a single empty line between two filled ones, it’ll skip over that and look for the next one.

Using the -e Flag

The -e flag finds the end of each line and adds a $ at the end of each line.

cat -e file1.txt
Linux Terminal Cat Command Flags E Flag

This flag makes it easier to work with your text files when you’re using regular expressions or regex. You can have an extra script or program read the contents of the saved file and check the ends using regex.

Do remember that the -e flag also adds a $ at the end of each empty line, so you might want to use this alongside -s to make thing look cleaner.

Using the -t Flag

The -t flag is similar to the -e flag, but looks for the “tab character” instead and replaces it with a ^I. What’s great with this flag is that it knows how to differentiate “tab” from the “double space” character.

cat -t file1.txt > file2.txt
Linux Terminal Cat Command Flags T Flag

This means that if you use the -n or -b flags alongside the -t flag, it’ll read the the space characters as they are and leave them be. The same goes if you add a ton of spaces to mimic a “tab” character.

Linux CAT Command Uses

The cat command should only be used when you’re trying to combine text files line by line. Don’t expect the cat command to take two lines of text and combine them into a single line.

Linux Terminal Hello World Cut Up Into Two Lines

On the other hand, you’ll see a lot of use for this when you’re trying to clean up text files for other scripts to work on. The -e, -t, -b, and -s flags are all useful enough to save you time from coding your own script for whatever these flags already do.

It also helps make it easier to view text files compared to having to use the Nano editor all the time.

Nano Editor Hello World In Two Lines

But if you’re trying to do something it’s not meant for, then it might be time to take a look at other ways to concatenate files on Linux.

Frequently Asked Questions

What will happen if I use the CAT command on an video file?

Video files contain a thing called “header.” This is a piece of information that shows how the video has been encoded. If you use the CAT command to concatenate videos, you’ll be left with one that has header information sandwiched between encoded video data. Most viewers will read this and think that your file has been corrupted. You’re better off using a different tool like FFmpeg to do this with video files.

Can I use the Linux CAT command with files from different folders?

You can treat your filenames as folder paths when you’re combining files in folders using the cat command. This way, you can combine a file in Desktop with another from Photos by doing it like this: cat Desktop/file1.txt Photos/Photos.txt >> output.txt.

What happens when I use an existing file's filename as output to the CAT command?

Depending on whether you are using > or >>, it will either overwrite or append the content to the existing file. The >> command appends to a file or creates the file if it doesn’t exist. The > command overwrites the file if it exists or creates it if it doesn’t exist.

To avoid any guesswork, it is always best to save the output to a non-existing file with unique filename.

Is this post useful?
Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Terenz Jomar Dela Cruz

Terenz is a hobbyist roboticist trying to build the most awesome robot the world has ever seen. He could have done that already if he wasn't so busy burning through LEDs as a second hobby.