How to use cat command in Linux operating system

how to use cat command in Linux operating system

In this article, we are going to show you that how to Use the cat command in Linux. If you want to learn the basic of the cat command in Linux then this article is ideal for you.

The cat command is very frequently used commands in Linux operating system. The name of the cat command discover from its functionality to concatenate files. It can reads data from the file and display the file content as output. You can also create, view, concatenate files using  cat command.

Linux is an operating system, like Windows OS, iOS, and Mac OS. Linux is one of the most popular operating system on the planet, Android is also powered by Linux OS(operating system). An operating system is software that manages all hardware resources associated with desktop or laptop. In simple words, the OS(operating system) manages communication between software and hardware.

Cat command in Linux operating system

In the Linux, the basic syntax of the cat command is show as below:

$ cat [OPTIONS] [FILENAME]

1) To view a single file content :

$ cat filename
Output
It will show content of given filename

2) Redirect Contents of File :

$ cat filename1.txt > filename2.txt

The above command will copy the contents of filename1.txt to filename2.txt using the (>) operator:

3) To view multiple files content :

$ cat filename1 filename2
Output
This will show the content of file1 and file2.

4) To view file content with Line Numbers :

$ cat -n filename
Output
It will show content with line number
example: cat-n  example.txt
1 This is a example
2 hello how are you

5) To suppress repeated empty lines in output

$ cat -s file.txt

6) To append filename1.txt to filename2.txt :

$ cat filename1.txt >> filename2.txt

7) To Display End of Lines :

$ cat -e filename

8) To Concatenating Files :

$ cat filename1.txt filename2.txt

The above command should read the contents of filename1.txt and filename2.txt and display the result as output.

$ cat filename1.txt filename2.txt > mergedfile.txt

The above command should concatenate the contents of filename1.txt and filename2.txt and write them to a new file mergedfile.txt.

If the mergedfile.txt file doesn’t exist in the Linux system, the above command will create it as new file. Otherwise, it will overwrite the existing file.

9) Display the content of all text files in the folder:

$ cat *.txt

10) To display content in reverse order using tac command:

 $ tac filename

That’s all

If you find any error and issue in above steps , please use comment box below to report

If our tutorials helped you, please consider buying us a coffee. We appreciate your support!

Thank you for your support.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top