The cat command in Linux is mostly used for displaying the content of the files and appending the content of one file to the other file. In this tutorial, we will cover the procedure of creating files in Linux using the cat command.
How To Create a Text File in Linux Using cat Command
You can create a file and add the text into the file through the cat command. Use the following syntax for creating the text file in Linux:
Let’s create a text file named a newfile.txt using the following command:
Once you executed the cat command the cursor will move to the next line where you can begin adding the text in the file:
Once you are done with writing the text in the file, press Ctrl + D to save the file in the device. Verify the created file by using the following command, it will read the content of the file and display it on the terminal:
How To Create a Bash File in Linux Using cat Command
The cat command can also be used for creating the bash file in Linux. Use the following command syntax on the terminal:
I have created a file2.sh via the cat command using the above given syntax:
Next, type the script you want to add in the bash file and display the file via the below given command:
If you want to run the script, use the following command:
How to Redirect the Content of File Using cat Command
The cat command can also be used for redirecting the content from the source file to the destination file. The following command will copy the content of newfile.txt to the file2.txt. If the file2 does not exist this command will create it:
How to Display Line Number with cat Command
Through the cat command you can also display the line number at the start of each line. Use the following command:
The cat is one of the most useful and powerful commands of Linux. It is used in Linux for creating, and displaying content of the files. Other than that, cat command can also be used for appending the content of one file to another. In this tutorial, we have discussed the creation of bash and text files in Linux using the cat command and we have also discussed the appending of content from the source to the destination file.