3 Ways to Create a Text File Quickly Through the Linux Terminal

Being a Terminal-savvy person, you may always be looking for ways to ditch the mouse. Creating a text file is one task for which you can depend only on your keyboard on an Ubuntu system. Three commands from the Linux command line are at your service for creating text files. These include:

  • The cat command
  • The touch command
  • The standard redirect symbol

Let us explore these commands in this article to create some sample text files. The commands and procedures mentioned in this article have been run on an Ubuntu 20.04 LTS system. Since we will be creating the text files using Ubuntu command line-the Terminal; you can open it either through the system Dash or the Ctrl+Alt+T shortcut.

The cat Command

The cat command is very helpful when dealing with text files in Linux. It helps you in achieving three basic purposes:

  • Creating a text file
  • Printing contents of a text file in your Terminal
  • Printing contents of a text file to another text file

Here, we will explore the first use of the cat command; creating a text file through the command line.

Enter the following command in your Terminal:

$ cat > filename.txt

After entering this command, the next prompt will not appear; rather the cursor will display for you to enter the text for the file you just created.

Example:

In this example, I have created a text file through the following command and then entered some sample text:

$ cat > SampleTextFile.txt

Create file with cat command

Once you have entered all the text, hit enter to move to the next line and then use the Ctrl+D control to tell the system that you are done with entering the text. The usual command prompt will then appear for you to move on with further operations.

You can then use the ls command to see that your newly created text file will be there in the system.

$ ls

Check file with ls

Through the cat command, you can then view the contents of the file as follows:

$ cat filename.txt

Example:

You can see that the cat command shows the text I wrote while creating my sample file:

View content of file with cat command

The touch command

Another way of quickly creating a text file through the Terminal is by using the touch command. The touch command, however, does not let you enter text in the file at the time of creation. After creating the file, you can enter the text through your favorite text editor. You might prefer the touch command over the cat command in one scenario; when you want to create multiple files at once through one command.

Let us first see how to create a single file first through the Linux touch command:

$ touch filename.txt

Example:

$ touch sampletouchfile.txt

Create file with touch command

Use the ls command to see if the recently created file now exists on your system.

Chcek created file with ls

Create multiple files at once through the touch command

As mentioned above, the touch command takes the lead on the cat command on the basis that you can create multiple files simultaneously through the former. Use the following syntax to do so:

$ touch filename1.txt filename2.txt filename2.txt ….

For example, in the following command, I have created three files at once through the touch command:

$ touch sampletouchfile1.txt sampletouchfile2.txt sampletouchfile2.txt

Create multiple files with touch command

I also checked the presence of the three files through the ls command in the above example.

If you want to edit any of the files you created through the touch command, you can use any of your favorite text editors. Here I am using the Nano editor to enter text to one of the files I created. I used the following command to open the file through the Nano editor.

$ nano sampletouchfile.txt

Check file content with nano editor

I then entered the text and saved it by pressing Ctrl+X and then by hitting Enter.

The touch command can also be used to change the access and modification time of a file.

Change the access time of a file:

touch -a samplefile.txt

Set the modification time of a file:

touch -m samplefile.txt

You can view the access and modification time of files with the stat command:

stat samplefile.txt

Using the Standard Redirect Symbol

The standard redirect symbol is usually used when redirecting the output of a command to a file. However, it can also be used to create a single text file. The only difference is that while creating a new file we do not specify any command before the redirect symbol.

The difference between using the standard redirect symbol for creating a text file is that, unlike the cat command, you can not enter text this way. Also, unlike the touch command, you can only create one file at a time through the redirect symbol.

Use the following syntax in order to create a text file through this symbol:

$ > filename.txt

Use > to create file

You can then use the ls command to see if the newly created text file now exists on your system.

New file shows up with ls

You can enter text in the file through your favorite text editor. In the following example, I am using the Vim editor to edit the file through the following command:

$ vim MyTextFile.txt

Check file with ls

When you save and exit the file, your text file will have those contents saved.

Through this article, we have learned three basic ways to create text files quickly through the Linux command line. You can now avoid the mouse and use only the keyboard in order to perform the simple task of creating a text file in Ubuntu.