cat Command in Linux



cat is a Linux command that allows users to view, create, merge, concatenate and manipulate file contents. By using the cat command on Linux, you can display the content of a single file, create new files directly from the terminal or combine the content of multiple files.

Table of Contents

Installation of cat Command in Linux

The cat command is typically installed on most Linux distributions. If it is unavailable, you can install this command from the coreutils package. The coreutils package includes cat utility and it can be installed through the default Linux package manager.

The coreutils package can be installed on Debian-based distributions from the apt package manager using the following command −

sudo apt install coreutils

For other distributions like Fedora, REHL, and CentOS, you can use any of the following commands to install coreutils package on your system −

sudo yum install coreutils

Or,

sudo dnf install coreutils

Syntax for cat Command in Linux

The basic syntax to use the cat command on Linux is given below −

cat [options] [file_names]

Different Options Available for cat Command in Linux

There are different options that you can use with the cat command, these are discussed in the table below −

Option Description
-e Displays invisible line ending characters.
-n Displays line numbers along with file contents.
-s Suppresses repeated empty output lines. Useful for concise output.
-T Visually distinguishes between tabs and spaces.
-v Shows non-printable characters (such as tabs, line breaks, etc.)

Examples of cat Command in Linux

There are multiple cases where you can use the cat command on your Linux system, these are discussed below −

View Content of a Single File

One of the basic functions of the cat command on Linux is to view the content of a file. You can do this by simply using the cat command without an argument followed by the file name. For example, let’s say we have a file named file.txt and to view the content of the file, the following command will be used −

cat file.txt
View Content of a Single File

View Content of Multiple Files

You can also use the cat command to view the content of multiple files with a single command. For that purpose the cat command will be used without an argument followed by the file names with a space in between them.

For example, we have two files named file.txt and file2.txt and to view the content of these files, use the below-given command −

cat file.txt file2.txt
View Content of Multiple Files

View Content of a File with Line Numbers

If you want to view the content of a file with line numbers for ease, you can use the -n option with the cat command. The following example with show you the content of a file named file.txt with the line numbers −

cat -n file.txt
View Content of a File with Line Numbers

Create a New File with Content

Apart from viewing the content, you can also create a new file and include content according to your choice. You can do this by using the cat command followed by the file name you want to create, let’s say we are creating newfile.txt, the command is given below −

cat > newfile.txt

When you execute the command, the file will be created, however, to put the content inside the file, you have to write it in the prompt section −

Create a New File with Content 1

Once, done, use the CTRL+D button to save the file.

Create a New File with Content 2

After this phase, your file will be created with content inside the file.

Copy Contents to Another File

With cat command, you can also copy the content of one file into another file. For this purpose, use the cat command followed by the source file name, then the output redirection sign (>) and destination file name in the last.

For example, let’s use the below-given command to copy the content of file.txt to file2.txt

cat file.txt > file2.txt
Copy Contents to Another File

Suppress Repeated Empty Lines

If there are repeated empty lines in a file and you want to suppress them, you can use the cat command with the -s option. This will display the file content without repeated empty lines.

For example, we have a file named test.txt that includes multiple empty lines. So, to compress these lines, we can use the following command −

cat -s test.txt
Suppress Repeated Empty Lines

Highlight End of Lines

You can also display invisible line ending characters at the end of each line (adds $ sign) by using the cat command with -e option. These characters, represented by a $ sign, help you visualize where a line ends. For example, the following command will highlight the end of lines in the file file.txt

cat -e file.txt
Highlight End of Lines

Concatenate Multiples Files

You can also use the cat command to concatenate multiple files and merge into a single file. This can be done using the >> option. For example, let’s concatenate files file.txt and test.txt into a single file named merge.txt using the following command −

cat file.txt test.txt >> merge.txt
Concatenate Multiples Files

You can also use the wildcard option * and add multiple files of the same format and merge them into a single file using the following syntax −

cat *.txt >> filename.txt

That’s how you can use the cat command to perform different activities on your Linux system.

Conclusion

cat is a versatile command on Linux systems that is used for viewing content of a file, or creating a new file and input content directly from the terminal. Apart from that, you can use it to concatenate files, merge them, highlight invisible line endings and even suppress repeated empty lines.

This tutorial covered the installation method of cat command, along with its syntax, examples and different available options for better understanding. Mastering the cat command can significantly enhance your file manipulation skills.

Advertisements