Skip to main content
Basics

Create Text Files in Ubuntu Command Line

Here are various ways of creating new text files in the Ubuntu command line. You can either create an empty file or add text to it.

Abhishek

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

The touch command in Linux is used for 'touching' a file and changing its timestamps (access time, modified time ,etc).

If you use the touch command with a file that does not exist, it creates a new file with no content.

touch new_file
Creating new file in Ubuntu command line with the touch command

Once the file is created, you can open the file for editing and add the desired text to it.

⚠️
You should pay attention while creating a new file. Careless use of some commands may change their content if a file with the same name already exists.

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.

Create new file with cat command in Ubuntu terminal

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.

Command Description
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.

nano

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.

Creating new file with Nano editor in Ubuntu

You can also use Nano to name a file and then open it for editing:

nano new_file

Read this article to learn more about using Nano to edit files in the command line.

How to Edit Files in Ubuntu Command Line
Stuck in the terminal and need to edit a text file? Here’s what you need to know about editing files in the Ubuntu command line.

Conclusion

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.