Creating a new text file takes only a couple of mouse clicks when using the desktop.
Things are not the same when you are stuck with the terminal and have to do everything using Linux commands. You don't have the luxury of a mouse here.
So, how do you create a text file in the Ubuntu command line? Let me share a few methods.
Create a new empty text file with the touch command
If you use the touch command with a file that does not exist, it creates a new file with no content.
Once the file is created, you can open the file for editing and add the desired text to it.
Create a new file with the cat command
Another common Ubuntu command for creating new files is the cat command.
The cat command is primarily used for viewing the contents of a text file. However, it can also be used for creating new files.
cat >> new_file
With the above command, you open the new_file in append mode. You can enter some text into the file directly. That's optional, though. You can exit the cat command append mode with Ctrl+D keys.
Using >> with the cat command is important here.
If a file with the given name already exists, you'll add the new text at the end of that file thanks to the append mode >>.
Using the single > will delete the existing content of the file. You don't want to delete the contents of a file accidentally, do you?
This chart will help you distinguish between similar cat command operations.
|cat filename||View the content of the file|
|cat > filename||Opens file in overwrite mode|
|cat >> filename||Opens file in append mode|
Create a new text file with the echo command
You may have encountered the echo command while learning the Linux command line. It simply echoes the command input on the screen.
The echo command can also create new files with a given text or an empty line.
echo "Hello World" >> new_file
The above command will create new_file (if it doesn't exist already) and add the text "Hello World" to it.
You can skip the text part, and it will create a new file (if it doesn't exist already) with a new line.
echo >> new_file
Create a new file with a text editor
All the above commands give you only limited abilities to add some text to the file.
You can create and edit a file in the command line with a text editor. Ubuntu comes with the Nano editor and you can use it to create a new file that will be instantly opened for editing.
The above command will open a new file without any name. You can enter some text to it and save it with Ctrl+X keys. When you save the file, it allows you to name it.
You can also use Nano to name a file and then open it for editing:
Read this article to learn more about using Nano to edit files in the command line.
As you can see, you have multiple ways to create a new file in the command line in Ubuntu.
The touch command is quick for creating new empty files. Nano is good for creating and editing files simultaneously.
You can choose the one that suits your purpose.
Since you have learned about creating new files, perhaps you should also learn about deleting files in Ubuntu.