Linux Commands

Linux Create Text File from Command Line

Text files are handy in storing the data. You can create a text file using the GUI or the CLI. Using the command line is the quickest way of creating and working with text files. If you are looking to sharpen your skills on how to create the text files from the command line, you are in luck.

Today’s post presents the different methods that you can use to create the text files. We will present each method and provide examples on how to implement it. Let’s begin!

How to Create Text Files from the Command Line in Linux

The command line is powerful, and you can quickly use it to create text files in Linux. Here, we will cover the different methods that you can use. That way, you will find the most ideal method that you can use for your case.

Method 1: Using the Touch Command

Linux offers the “touch” command to help the users create files. You can use it to create your text file. The only drawback is that it doesn’t allow you to add a content to the created text file. Nonetheless, here’s how you can use it to create a text file:

$ touch filename

For instance, let’s create a “file1.txt” as shown in the following:

Verify that our text file is created using the “ls” command.

Suppose we want to create numerous text files. We only need to specify their names separated with a space as shown in the following:

We can confirm that we managed to create the different text files with one command.

Method 2: Using the Redirect Symbol (>)

With the standard redirect symbol in Linux, it’s possible to create a text file. You only need to add the redirect symbol and the name of the target text file that you want to create.

$ > filename

Watch how we create the “file1.txt” using the redirect symbol:

If you want to create numerous text files, add their names that are separated by the redirect symbol as shown in the following:

We confirm that all our target files have been quickly created using the redirect symbol. It’s that simple!

Method 3: Using the Echo and Printf Options

We mainly use the “echo” command to display something on the terminal. You can also use it with the redirect symbol to create the text files. The “echo” and “printf” commands work the same. You have two ways which you can use when you want to create the text files with the “echo” and “printf” commands.

First, when you want to create a blank text file, you only specify its name after the double redirect symbols. Let’s create the “courses.txt” with the “echo” command. For that, our command is as follows:

$ echo >> courses.txt

When you run the “ls” command, you can confirm that we managed to create our target text file.

If we were to use the “printf” command, we must add the quotes before the redirect symbol. Use the following syntax to create your text files:

$ printf “” >> filename

When the command runs, your text file will be created in the current directory.

With “echo” and “printf”, you can also create multiple text files simultaneously. For that, specify their names as follows:

For echo, add the names separated by the redirect symbols.

We confirm that all the multiple text files have been successfully created.

With printf, follow the same criteria and ensure that you have the quotes at the start. You will also manage to create your desired text files.

The second option of creating the text files using “echo” and “printf” is when you want to create a text file and add contents to it.

With “echo”, add the -e flag and the line of text to be contained. Then, use the redirect symbol to add the contents to the file that you want to create.

Here’s an example:

When you open the created text file, using an option like the “cat” command, you will confirm that the file is created and contains the desired contents.

If using printf, the syntax slightly changes. Watch how we implement the following example:

Using the “cat” command, we can open the file and confirm that it contains the contents that are specified when creating it.

Method 4: Using the Cat Command

There are different ways where you can use the “cat” Linux command. Use the following syntax when you want to create a text file:

$ cat >> filename

Note that using this option gives you room to add a content to your text file. Once you add the desired content, press “Ctrl + c” to close the file.

We managed to create the target file, and we can verify that it contains the contents that we added when creating it.

Conclusion

In Linux, there are different ways where you can create the text files. This post focused on the four main ways where you can create the text files using the command line. Hopefully, you found a method that suits your case.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.