Using the cat Command in Linux

In this guide, we will teach you how to use the cat command on your Linux-based system.

cat Command on Linux

cat is a powerful tool that can do various tasks despite the name being short for “concatenate”.

The cat command allows you to read, concatenate, create and modify files on your Linux system.

This tool is a core part of Unix, meaning that any Linux or Unix-based system should have access to the cat command.

Below, we will walk you through the various ways that you can utilize this command.

If you would like to follow along with our example files you can download the text files from our CDN.

Table of Contents

Syntax of the cat Command

The cat command is reasonably easy to use. While it only has some basic functionality, it is incredibly versatile.

The syntax for the cat command is simple. It is “cat” followed by options, followed by the name of any files you want to concatenate.

cat [OPTION]... [FILE]...

The option ([OPTION]) flags allow you to modify the behavior of the cat command. Most of the options are used to control the output produced by the cat command. You are not required to enter any options.

Finally, the file flag ([FILE]) allows you to specify any files you want to read or concatenate. If you don’t pass a filename, the command will read its data from standard input instead.

You can also utilize a dash (-) for the filename to replicate the same behavior as specifying no filename.

Displaying the Contents of a Single File using cat

You can use the cat command to output the contents of a single file to the command line.

All we need to do is use “cat” followed by the file’s name that we want to view the contents of.

cat FILENAME

Using this command, the contents of the specified file will be output to the terminal.

Example of Outputting the Contents of a Single File

To show you how this works, we will be outputting the contents of an example file with the name “example1.txt“.

This is as simple as using the following command within the terminal. You can see we used “cat” followed by the filename.

cat example1.txt

By using this command, the following text was output to the terminal.

pi
my
life
up

Outputting the Contents of Multiple Files with the cat Command

The cat command also allows you to combine (concatenate) the contents of multiple files and output the result.

To concatenate multiple files, you need to specify each file’s name. Ensure a single space separates the names of each file.

cat FILENAME1 FILENAME2

Even though our example only shows two filenames, you can concatenate the contents of as many files as you want.

Example of Outputting the Contents of Multiple Files

We will be passing in the filenames of two separate files with the cat command. The tool will work by combining each of these files then outputting the result.

Here we use the cat command followed by the names for our two examples files (example1.txt and example2.txt).

cat example1.txt example2.txt

Below you can see that our “example1” text file was now output with the contents of our “example2 text file.

pi
my
life
up
pimylifeup
example text
for the cat command

Including Line Numbers with the Output

The cat command on Linux also can output line numbers. This is useful if you want to easily check how many lines in a certain line or phrase is.

All we need to do is use the “-n” (--number) option to tell the cat command to output the line number on every line.

cat -n FILENAME

Example of Including Line Numbers

For example, let us use the “-n” option to output the line numbers for our example file called “example1.txt“.

All we need to do is use “cat” followed by “-n“, then finally the filename we want to output the line numbers with.

cat -n example1.txt

As you can see, the line number is now included alongside every line from our file.

     1  pi
     2  my
     3  life
     4  up

Remove Repeated Empty Lines using the cat Command

The cat command allows you to suppress repeated empty lines from its output. This functionality helps clean up files that have numerous repeated empty lines.

We can achieve all of this by utilizing the “-s” (--squeezeblank) option alongside your call to the cat command.

cat -s FILENAME

Example of Suppressing Repeated Empty Lights

To show you how this works, we will be using our third example file called “example3.txt“. Within this file, we have intentionally added extra empty lines.

All we need to do is use “cat“, followed by the “-s” option, then the name of our file.

cat -s example3.txt

This command will take some text like the following and strip out any repeated empty lines.

showcasing what happens with


multiple blank lines that are uneeded




and using the -s option pimylifeup

After running the command your data will end up looking like what we have shown below. All occurrences of multiple empty lines have been squished down to a single one.

showcasing what happens with

multiple blank lines that are uneeded

and using the -s option pimylifeup

Highlight the End of Lines

Built into the cat command is the ability to highlight the end of lines. The tool adds a dollar sign symbol ($) to the end of every line.

Using this, you can see lines that might have extra spaces that you typically wouldn’t see.

All we need to do to achieve this is to use the “-e” (--show-ends) option alongside the “cat” command.

cat -e FILENAME

Example of Highlighting the End of Lines

Highlighting the end of lines is a straightforward task. We will be using a file called “example1.txt” for our example.

We need to use “cat” followed by the “-e” option and, finally, the file name.

cat -e example1.txt

Using this command, you will have text output to your terminal. You should now see a dollar sign ($) at the end of each line

pi$
my$
life$
up$

If you instead see “^M$” in place of the dollar sign, this means your file contains carriage return characters. This is typically how Windows handles its line endings. You can use a tool such as Dos2Unix to convert these line endings.

Show Tabs Within a File Using the cat Command

The cat command also can replace any tabs within the file with “^I“. This helps with working out where tabs have been used in a file instead of spaces.

To get the cat command to replace the tabs within its output, we need to use the “-T” (--show-tabs) option.

cat -T FILENAME

Example of Showing Tabs Within a File

For this example, we will be using a file called “example4.txt” that has been intentionally filled with tabs.

To process this file, we can use the following command. First, we use the “cat” command followed by the “-T” option than our filename.

cat -T example4.txt

From the result of this command, you can see that all the tabs have now been replaced with “^I“.

hello^Iworld^I^Ipimylifeup

^Iexample^I^Ifile

filled^I^Iwith^I^Itabs

Creating a New File Using cat

You can even use the cat command to create a new file by using Linux’s output redirection (>) to save the contents to a new file.

Please note that when used in this case the “>” operator will completely overwrite the contents of the file.

The syntax for creating a new file is simple as using “cat” followed by output redirection, then finally the same

cat [OPTIONS]... [INPUTNAME]... > OUTPUTNAME

After running the command, it will await your input. Anything you type while within this input will be output to the specified file.

To exit the cat command, all you need to do is press CTRL + D on your keyboard.

Example of Creating a New File Using the cat Command

Creating a new file with the cat command is incredibly simple, as all you need to know is the file name.

1. To showcase this, let us quickly create a new file called “catexample” by using the following command.

cat > catexample

2. After running the command, you can now type text into the command line.

cat will save any text you write here to the file thanks to us using the output redirection symbol.

For our example, we will be entering the following super simple lines of text.

up
life
my
pi

3. Once entered, quit the interactive cat terminal by pressing CTRL + D.

Appending Contents From cat to Another File

In our previous example, we used the “>” operator to replace file’s contents using output from the cat command.

If you want to append the results of the cat command instead of replacing the file, we can use the “>>” redirection operator.

Below you can see the syntax of how you can append the results

Below is the basic syntax for appending the result from cat onto the end of another file.

cat [OPTIONS]... [INPUTNAME]... >> OUTPUTNAME

You can also use this same method to use the cat command to join one file onto another.

Example of Appending Text to Another File

We will show you how we can append the text from standard input to a file for this example.

1. Using the following command, we will be appending text to the file name “catexample“.

We use cat followed by the “>>” redirection operator then the files name we are writing to.

cat >> catexample

2. After running the command, you can now enter text. Any text you write will be appended to the end of the specified file.

We will add the following lines to the end of our example file.

pi
my
life
up

Once you are done typing in text, press CTRL + D to exit from the interactive command line.

3. If you used cat on “catexample” you will end up with the following text in your terminal.

up
life
my
pi
pi
my
life
up

Example of Appending a File to Another with the cat Command

You can also use the cat command to append one file to another. This result would be similar to combining multiple files as an input to the cat command.

For this example, we will be using cat to append two files. In our case, this will be the “example1.txt” and “example2.txt” files.

cat example1.txt >> example2.txt

Using the command above, our resulting file will end up like what we have shown below.

The contents of the file called “example1.txt” are now appended to the end of the content from “example2.txt“.

pimylifeup
example text
for the cat command
pi
my
life
up

Conclusion

Hopefully, by this point in the guide, you will have learned how to use the cat command on your Linux system.

As you have seen, you can use this command in several different ways. For example, you can create files, append content to existing ones and concatenate several files together.

If you have had any issues with using the cat command, feel free to comment below.

Be sure also to check out our other Linux tutorials and command guides.

Leave a Reply

Your email address will not be published. Required fields are marked *