Home Linux 5 Basic Examples of Cat Command in Linux

5 Basic Examples of Cat Command in Linux

What is the purpose of Cat command in Linux?

What is Cat Command?

Cat command stands for concatenate. It is a widely used command in the Linux systems. Cat command in Linux used to view the contents of a file, create a file and copy the contents of one file to another file.

In this article, we will let you know examples of commonly used cat command.

1.View Contents of a File

Below command display the contents of a file /etc/passwd on the terminal.

cat /etc/passwd
View contents of a file
View contents of a file

2. Display Contents of Multiple Files

For example, we wanted to see the contents of two files i.e. project and staging.

Read More: 7 Examples of Rsync Command

cat project staging
Display contents of multiple files
Display contents of multiple files

3. Show Line Numbers in a File

To view the line numbers in a file, -n option is used. As shown in the below example.

cat -n project
View line number in a file with cat command
View line number in a file with cat command

4. Create a File using Cat Command

We have created a new file test1 using the following command.

cat >test1
Create a new file using cat command
Create a new file using cat command

As you run the above command, it will wait for the input. Enter the desired text to store in a file. After that press Ctrl+D to exit. You can view the content via running below command.

cat test1

5. Copy Contents of a File Into Another File

With the following command, we can copy the contents of a file test1 into the test2 file.

cat test1 > test2

Bur Careful with this command, it will replace all the contents in the test2 file with the test1 file contents. Here is another scenario available.
If you want to copy the contents of the test1 file at the end of the test2 file. Then use the following command.

cat test1 >> test2

NOTE: Be careful before executing this command. Because of a little mistake in > (greater than) or >> ( double greater than ) symbol can bring you in trouble.

Read More: Linux Basic Commands with Examples


Conclusion: You have perfectly learned cat command used in Linux with the help of examples. In case of any confusion, kindly feel free to write in a comment box.

LEAVE A REPLY

Please enter your comment!
Please enter your name here