Cat command in Linux with examples

The cat command in Linux is our primary tool for viewing the contents of text files on Linux systems. When using or administrating a Linux system, you are usually confronted with a command line. There are no graphical aids such as mouse or windows to help you navigate through directories or edit files.

All Linux systems consist of many different configuration files. By editing or creating these files, an administrator is changing the behaviour of various services available within the system. Another situation where you can encounter text files are log files. Log files are produced by services running on the system. Information stored in the log file is there for the administrator to help troubleshoot and optimize running services.

Whether we are talking about Linux log files or configuration files, they are all simple ASCII text files. Therefore, the skills to be able to read the content of such text files is imperative. In this guide, you’ll see how to use the cat command through examples to view text files on Linux. We’ll also go over some of its most frequently used options so you’ll be armed with all the knowledge necessary to use this command effectively.

In this tutorial you will learn:

  • How to use the cat command on Linux
Cat command in Linux with Examples
Cat command in Linux with Examples
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software cat
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Frequently Used Options

The cat command works by concatenating files and printing their contents to standard output. Check some of the example commands below to see how it works.

Cat command in Linux basic usage example
Cat command in Linux basic usage example

Cat command in Linux Basic Examples

  1. Normally, you won’t specify any extra options with the cat command. All you need to do is specify the name of the file you wish to view, and the contents will be printed to your terminal.
    $ cat file.txt
  2. If you have more than one file you want to view, you can specify multiple file names in your command. Their contents will be concatenated and sent to your screen (standard output).
    $ cat file1.txt file2.txt
  3. Concatenation can also extend its usage with redirection. Suppose, that we want to create a new file file3 which will contain the content of both files, file1 and file2. In this situation, we use the cat command to concatenate content of both files and then use the > operator to redirect standard output to a file called file3.
    $ cat file1 file2 > file3

    After execution of this command, no output is presented on the screen. This is perfectly normal considering that we have used the > operator to redirect the output from the cat command to yet another file called file3. When we now try to read the content of file3 with the cat command the output produced should contain the combined content of file1 and file2:

    $ cat file3
    This is content from file1
    This is content from file2

    Please note that it is not important to create a file3 prior to redirecting an output into it. If the file3 would exist it will be overwritten and if the file does not exist it will be created.

  4. One of the useful options with cat is -n, which will create line numbers in the output.
    $ cat -n file3
         1	This is content from file1
         2	This is content from file2
  5. And another useful option is -s, which will suppress output of repeated empty lines. Empty lines are usually not very useful in your output, so this can make things much more concise.
    $ cat file
    Start of file
    End  of file

    And now the same file but with -s option:

    $ cat -s file
    Start of file
    End  of file

Advanced Usage

The cat command doesn’t really get very advanced. It’s really a pretty simple command, as you saw in the above examples. However, as with any command in Linux, you can chain it with other commands and pipes to create some additional complexity. cat can also be used to create new files. This is also not very advanced, but it’s at least uncommon and probably the most advanced usage of cat that you would need to know.

Cat command in Linux Advanced Examples

  1. Another approach where the concatenated output from a cat command can be used is with a combination of | (pipe) operator. Pipe operator can be used to redirect output of one command as an input to other commands. In the next example, we are going to use a sort command which will alphabetically sort any data.
    $ cat file1 file2 | sort
  2. When the cat command does not contain any arguments, it waits for an input from your keyboard. If you try to run the cat command lacking any arguments, cat will wait for your input from the keyboard until it receives an end-of-file ( EOF ) signal produced by CTRL+D key combination. When entering some input from a keyboard, cat command will simply repeat any input and display it on the screen.
    $ cat
    keyboard input
    keyboard input
  3. This keyboard input can be redirected directly into a file with the > operator. The following example illustrates this idea:
    $ cat > file
    Some example text
    $ cat file
    Some example text

    After entering the “Some example text” line, ENTER key was pressed to produce a new line character, followed by CTRL+D, which produces an end-of-file signal. Reading the content of file with the cat command confirms that the input from the keyboard was undeniably redirected into the file.

  4. As it was already mentioned above, a > operator will overwrite a content of a file. To append a new line, a >> append operator needs to be used instead. Consider the following example:
    $ cat file
    Some example text
    $ cat >> file
    Some more example text
    $ cat file
    Some example text
    Some more example text

    The procedure of inserting a new line with >> append operator is exactly the same is it was in the previous example. The only distinction is that >> will add a new line after the existing EOF character.

Closing Thoughts

In this guide, we saw how to use the cat command in Linux. This included frequently used options as well as advanced usage. cat is an essential command to master on Linux because it allows us to view all kinds of text files, but it’s also one of the simplest commands you’ll come across. After reading this guide, you should have all the knowledge of this command that you’ll ever need.