How to use the CAT command in Linux

0 comments 4.1K views 3 minutes read
Published on: August 6, 2022 | Last updated on: October 11, 2024

Using real-world examples, we’ll teach you how to use the cat command in this article.

One of the most used commands in Linux is cat. There’s nothing to worry about when you don’t provide an input file name or one that begins with a single “-” (hyphen).

Most often, cat is used to show the contents of text files, combine files by adding the contents of one file to the end of another, and create new files.

CAT Command Syntax

Starting with the syntax, let’s go over how to use the cat command.

This is the form of the cat’s utility expression:

cat [OPTIONS] [FILE_NAMES]
  • OPTIONS – cat options . Use cat –help to view all available options.
  • FILE_NAMES – Zero or more file names.

Retrieving a File’s Contents

Using the cat command to read a file’s content is the most common and fundamental use of the command.

As an example, using the following command will display the contents of the file /etc/issue on the terminal:

cat /etc/issue

Redirect the content of a file

It is possible to direct the output to a file rather than to stdout.

The (>) operator is used to copy the contents of doc1.txt to doc2.txt.

cat doc1.txt > doc2.txt

Normally, you’d use the cp command to copy a file to your computer.

The command will create doc2.txt if it doesn’t already exist. The file will be overwritten if it isn’t.

Append the contents of doc1.txt to doc2.txt using the (>>) operator:

cat doc1.txt >> doc2.txt

The file will be created if it doesn’t already exist, just like before.

Print the Line Numbers

The cat command can be used to display the contents of a file with line numbers:

cat -n /etc/lsb-release
1	DISTRIB_ID=Ubuntu
2	DISTRIB_RELEASE=20.04
3	DISTRIB_CODENAME=bionic
4	DISTRIB_DESCRIPTION="Ubuntu 20.04.1 LTS"

Eliminate Repeated Blank Lines

Use the -s option to omit empty output lines repeated:

cat -s doc.txt

Display TAB characters

The -T option distinguishes tabs from spaces visually.

cat -T /etc/hosts
127.0.0.1^Ilocalhost
127.0.1.1^Iubuntu1804.localdomain

The TAB characters are rendered as ^I.

Display Line Breaks

Line ending characters can be displayed with the -e argument:

cat -e /etc/lsb-release
DISTRIB_ID=Ubuntu$
DISTRIB_RELEASE=20.04$
DISTRIB_CODENAME=bionic$
DISTRIB_DESCRIPTION="Ubuntu 20.04.1 LTS"$

Endings of lines will be shown in the form of “$”.

Putting Files Together

When giving two or more file names as inputs to the cat command, the file contents are concatenated. cat reads and displays file contents in the order specified by its arguments.

For instance, the following command will read doc1.txt and doc2.txt and display the results on the terminal:

cat doc1.txt doc2.txt

You can concatenate many text files into a single file.

Using the (>) operator, the following command will append the contents of doc1.txt and doc2.txt to a new file named combineddoc.txt:

cat doc1.txt doc2.txt > combineddoc.txt

If combineddoc.txt does not already exist, the command will create it. Otherwise, the file will be overwritten.

To use the (>>) operator to concatenate doc1.txt and doc2.txt and add the result to file3.txt:

cat doc1.txt doc2.txt >> file3.txt

The file will be created if it does not already exist.

When cat is used to concatenate files, you can use the same parameters as in the preceding section.

File-Creating

Creating small files using cat is often more convenient than using a text editor like nano, Vim, Sublime Text, or Visual Studio Code..

In order to create a brand-new file, you must first run cat and then specify its name using the redirection operator (>). To save the file, press CRTL+D after you’ve entered the text.

Doc1.txt is being created in the following example.

cat > doc1.txt

If there is a file named doc1.txt, it will be deleted. To append the output to an existing file, use the ‘>>’ operator.

cat >> doc1.txt

Conclusion

The cat command has the ability to show, combine, and create brand new files in your computer.

Let us know if you have any questions or comments.