Top 12 Basic Linux Commands | Cat Command Examples

Basic Linux Commands the cat command is one of the most generally used commands in Linux and Unix operating system. Cat commands allow us to create single or multiple files, add files, combine files, and redirect output to the terminal or files. In this article, we are going to learn easy access basic Linux commands to cat commands with their examples.


Top 12 Basic Linux Commands | Cat Command Examples
Cat Command Examples


1. Display the contents of the file

In the example below, it will display the content of the /etc/passwd file.

 # cat /etc/passwd                                     
 root:x:0:0:root:/root:/bin/bash                 
 bin:x:1:1:bin:/bin:/sbin/nologin               
 narad:x:500:500::/home/narad:/bin/bash 


2. View the contents of multiple files in the terminal

Below the example, it will display the contents of the test and test1 file in the terminal.

 # cat test test1      
 Hello everybody  
 Hi, world,             


3. Create a file with the cat command

We will prepare a file called test2 file with the command below.

 # cat >test2 

Waiting for the input from the user, type the text you want and press CTRL + D to exit (press the Ctrl key and type 'd'). The text will be written in the test2 file. You can see the contents of the file with the following cat command.

 # cat test2                                       
 hello everybody, how do you do?  


4. Use cat command with fewer options

If the file contains the large number of content that will not fit in the output terminal and the screen scroll, then we can use the parameter with the cat command as the above show.

 # cat song.txt | more  
 # cat song.txt | less    


5. Display the line number in the file

With -n option you can see the line number of the file song.txt in the output terminal.

 # cat -n song.txt                  
 1 "Hello Beautiful World"  
 2 Example song name 1      
 3  Example song name 2     
 4  Example song name 3     
 5  Example song name 4     
 6  Example song name 5     
 7  Example song name 6     
 8  Example song name 7     
 9  Example song name 8     
 10  Example song name 9   


6. Display $ at the end of the file

In the below given below, you can see with -e option that the '$' is shown at the end of the line and if there is any difference between the paragraphs then the '$' can also be shown in the space shown. This option is useful for squeezing multiple lines in one line.

 # cat -e test                                       
 hello everybody, how do you do?$  
 $                                                        
 Hey, I'm ok.$                                    
 How's your education going on?$    
 $                                                        


7. Display Tab separated Lines in File

In the output below, we can see that the tab space is full of letters '^ I'.

 # cat -T test                                           
 hello ^|everybody, how do you do?      
 Hey, ^|I'm ok.                                        
 ^I^IHow's your education ^Igoing on? 
 Let's do ^Isome exercise in Linux.       


8. Display multiple files at once

In the example below, we have three files Test, Test 1 and Test 2 and are able to see the contents of the files shown above. We need to separate each file; (Semicolon).

 # cat test; cat test1; cat test2  
 This is a test file                     
 This is a test1 file.                  
 This is a test2 file.                  


9. Use standard output with redirection operator

We can redirect the file's standard output to a new file and with the existing file '>' (plus) symbol. Carefully, the existing content of Test 1 will be overwritten by the contents of the test file.

 # cat test > test1 


10. Attaching standard output with redirection operator

Adds '>>' (more than double) symbols to the existing file. Here, the contents of the test file will be attached at the end of the test1 file.

 # cat test >> test1 


11. Standard input redirection with redirection operator

When you use the redirect with the standard input '<' (less than the symbol), it uses the file name as input for the command as test2 and the output will be displayed in the terminal.

 # cat < test2            
 This is a test2 file.  


12. Redirecting multiple files to a file

This will create a file named test3 and all the output will be redirected to a newly created file.

 # cat test test1 test2 > test3 

One bonus command for you


Sorting Contents of Multiple Files in a Single File

This will create a file test4 and the pipe has been piped to sort the output of the cat command and the result will be redirected to a newly created file.

 # cat test test1 test2 test3 | sort > test4 

This article shows Basic Linux Commands that can help you find cat commands. If you want to know more, you can just write a comment. In our next article, we will cover more advanced and your comment suggests cat commands. If you lick this article, So just write your Experience through our comment box below. And please share it.

Post a Comment

0 Comments