Cat Command in Linux

In this tutorial, you’ll explore how to use the cat command in Linux. cat is commonly used to display the contents of one or more text files, combine files, and create new files by appending the contents of one file to the end of another file.

The cat command is one of the most used commands in Linux. The name of the cat command comes from its functionality to concatenate files. You can read, append, and write the contents of the file to standard output. If no file is specified or if the input file name is specified as a single hyphen (-), it is read from standard input.

Options

TagDescription
-A, –show-allequivalent to -vET
-b, –number-nonblanknumber nonempty output lines override -n
-eequivalent to -vE
-E, –show-endsdisplay $ at end of each line
-n, –numbernumber of all output lines
-s, –squeeze-blanksuppress repeated empty output lines (never more than one single blank line)
-tequivalent to -vT
-T, –show-tabsdisplay TAB characters as ^I
-u(ignored)
-v, –show-nonprintinguse ^ and M- notation, except for LFD and TAB
–helpdisplay this help and exit
–versionoutput version information and exit

cat command syntax

Before we explain how to use the cat command, let’s first review the basic syntax.

Cat utility expressions take the following form:

cat [options] [FILE_NAMES]
  • Options – Chat options. Use cat –help to see all available options.
  • FILE_NAMES – Zero or more file names.

Displaying File Contents

The most basic and common usage of the cat command is to read the contents of files.

For example, the following command will display the contents of the /etc/lsb-release file on the terminal:

cat /etc/lsb-release

Create a file with cat

To create a file, we’ll use the cat command input stream and then redirect the cat command’s output to a file using the Linux “>” redirection operators.

Specifically, to write to a file using the cat command, we enter this command in our terminal:

cat > file.txt

We will see that once again the terminal is waiting for our input. However, this time it will not echo the text we typed. This is because we requested that the output be redirected to the file.txt file instead of the standard output stream.

Enter some text in the Terminal followed by CTRL+D to complete the command:

cat > file.txt
This is a file.
This is a new line.

The file.txt file will now contain the two lines we entered. To check our result, we can again use the cat command:

cat file.txt
This is a file.
This is a new line.

So! We wrote to a file using the cat command.

Add text to a file using Cat

One thing we should note in the previous example is that it will always overwrite the file.txt file.

If we want to append to an existing file, we can use the “>>” operator:

cat >> file.txt
This is an attached line.

To verify that the last command added the file, we examine the contents of the file:

cat readme.txt
This is a file.
This is a new line.
This is an attached line.

Here we have it. The line we enter is appended to the end of the file instead of replacing the entire document.

Conclusion

We show you how to use the cat command in Linux. The cat command can display, combine and create new files. For more information on the cat command, see the man page for the cat command.

If you have any questions please leave them in the comments below

Leave a Reply

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