As there are several ways to do a single task in a Linux system. The same is also true for creating a file in Linux. In Linux, you can create a file using multiple ways including both the command line and the graphical ways. Some of the commands that are used to create a file in Linux include touch, cat, and echo commands. You can also use the text editors like Nano, Vim, and Gedit to create a file in Linux.
Today’s post is about how to create a file in Linux using the cat command. This is a quick method for those who want to quickly create a new file or add some text to their files.
Note: The method outlined here is focused on Linux distribution Ubuntu, but you can follow the same method if you’re using any other Linux distribution.
To create a file in Linux using the cat command, type cat followed by > operator and the filename as follows:
$ cat > filename
For instance to create a file named testfile.txt, use the cat command as follows:
$ cat > testfile.txt
After running the above command, you will not be returned to the Terminal prompt. Instead, Terminal will wait for your input. Type the text you want to save in this file. Hit Enter after typing each line. Once you entered all the text, hit Ctrl+D to save and exit.
Now to verify if the file has been created, run this command:
$ ls
This command lists all the files in the current working directory. In the output of this command, you will find the testfile.txt that you have created using the cat command.
After verifying the file creation, you can also view its content using the command below:
$ cat testfile.txt
This command will show you the text that this file testfile.txt contains.
After creating the file, you may need to add more text to the file. You can also do this using the cat command. For instance, to add more text to the file testfile.txt, type cat followed by the >> operator and the filename that you want to edit:
$ cat >> testfile.txt
Now add the text you want to append in the file. This command will append the text in the testfile.txt file. It will not overwrite the already added text. Once you have added the text, hit Ctrl+D to save and exit the file.
Now to verify if the file has been appended, use the command below:
$ cat testfile.txt
This command will show you the text that has been appended in the file testfile.txt.
Note: If a file already contains the text, using the cat command with the >> operator will append the text in the file. It will not replace or overwrite any existing text in the file. While using the cat command with > operator will overwrite the text in the file.
There you have how to create a file in Linux using the cat command. Using the cat command, you can also create a file, view the content of a file without opening it, and concatenate multiple files.
Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications including CCNA RS, SCP, and ACE. As an IT engineer and technical author, he writes for various websites.
One of the most critical gaps in traditional Large Language Models (LLMs) is that they…
Canonical is continuously hiring new talent. Being a remote- first company, Canonical’s new joiners receive…
What is patching automation? With increasing numbers of vulnerabilities, there is a growing risk of…
Wouldn’t it be wonderful to wake up one day with a desire to explore AI…
Ubuntu and Ubuntu Pro supports Microsoft’s Azure Cobalt 100 Virtual Machines (VMs), powered by their…
Welcome to the Ubuntu Weekly Newsletter, Issue 870 for the week of December 8 –…