3 ways to quickly create a text file using the Linux terminal

s2 0

As a terminal savvy person, you may always be looking for ways to get rid of the mouse. Creating a text file is a task that you can only rely on your keyboard for on an Ubuntu system. There are three commands from the Linux command line that you can use to create text files. These include:

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

Let’s examine these commands in this article to create some sample text files. The commands and procedures mentioned in this article were written on a Ubuntu 20.04 LTS System. Since we’re going to create the text files using the Ubuntu command line – the terminal; You can either open it from the System Dash or the Ctrl + Alt + T keyboard shortcut.

The cat squad

The cat command is very useful when dealing with text files on Linux. It will help you achieve three basic goals:

  • Create a text file
  • Printing the contents of a text file in your terminal
  • Print the contents of a text file to another text file

Here we will examine the first use of the cat command; Create a text file from the command line.

Enter the following command in your terminal:

$ cat> filename.txt

After entering this command, the next prompt is not displayed; Instead, the cursor appears for you to enter the text for the file you just created.

Example:

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

$ cat > SampleTextFile.txt

When you’ve entered all of the text, press Enter to go to the next line, and then use the Ctrl + D to let the system know that you have finished entering text. The usual command prompt appears so that you can continue with further operations.

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

$ ls

advertising

You can then use the cat command to display the contents of the file as follows:

$ cat filename.txt

Example:

You can see the cat command is showing the text I wrote when creating my sample file:

Use the cat command to view the contents of the file

The touch command

Another way to quickly create a text file through the terminal is to use the touch command. However, the touch command does not allow you to enter text into the file at the time of creation. After you’ve created the file, you can enter the text using your favorite text editor. In one scenario, you might prefer the touch command to the cat command; when you want to create multiple files at the same time with one command.

First, let’s see how to first create a single file using the Linux touch command:

$ tap 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 the same time using the touch command

As mentioned above, the touch command takes the lead over the cat command, as the former allows you to create multiple files at once. Use the following syntax to do this:

$ tap Filename1.txt Filename2.txt Filename2.txt ….

For example, in the following command I created three files at the same time using the touch command:

$ touch sampletouchfile1.txt sampletouchfile2.txt sampletouchfile2.txt

Create multiple files with touch command

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

If you want to edit any of the files created with the Touch command, you can use one of your favorite text editors. Here I am using the nano editor to enter text into one of the files I have 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 pressing 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 example file.txt

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

stat samplefile.txt

Use the standard redirect symbol

The standard redirection symbol is typically used when the output of a command is redirected to a file. However, it can also be used to create a single text file. The only difference is that when we create a new file, we don’t put a command in front of the redirect icon.

The difference between using the standard redirect symbol to create a text file is that, unlike the cat command, you cannot enter text this way. In addition, in contrast to the touch command, you can only create one file at a time using the redirection symbol.

Use the following syntax to create a text file from this symbol:

$ > filename.txt

Use> to create a file” width =”319″ height =”45″ srcset =”https://priyahomestore.com/wp-content/uploads/sites/10/2021/09/1630681574_182_word-image-129.png 319w, https://vitux.com/wp-content/uploads/2018/10/word-image-129 -300×42.png 300w” sizes =”(max-width: 319px) 100vw, 319px”><span id=

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

New file is displayed with ls

You can enter text into the file using your preferred text editor. In the example below, I’m using the Vim editor to edit the file with the following command:

$ vim MyTextFile.txt

Check the file with ls

When you save and exit the file, this content will be saved in your text file.

In this article, we learned about three basic ways to quickly create text files using the Linux command line. You can now avoid the mouse and just use the keyboard to perform the simple task of creating a text file in Ubuntu.

3 ways to quickly create a text file using the Linux terminal
Tags: