How To Create Text Files with the Command Line in Linux

Linux distributions have been initially thought to be managed through console or command line, so it is practical and necessary to know some ways to handle simple tasks through it.

One of these tasks is to create text files with some useful information and one of the best options to achieve this is through the cat command, and today we will analyze how to make it quickly.

Create a Text File Using the Cat Command in Linux

This method gives us the possibility to create text files with the cat command, and it is practical if we want to add text to the new file to be created immediately.

    1. For this, we are going to enter the following command in the terminal:
      cat > file . txt
    2. After pressing Enter, it will not return to the indicator of the terminal of the selected distribution, but the cursor will be located in the following line, and there we can begin to enter text directly in this file..
    3. There we will enter the text lines that we want, pressing Enter after each line. When finished, press the key combination Ctrl + D to exit the file and return to the created message.
    4. To verify that the file was created, we can use the ls command to display a directory list for the current file:
      ls -l file.txt

    5. There we will see details such as “Date and time of creation of the file” & “Assigned permissions.”
    6.  The cat command will also be useful to look at the contents of the created file, just execute the following command.
      cat file.txt


Create a Text File using the Touch Command in Linux

Another option that Linux offers us to create text files is using the touch command, and one of the differences between using the touch command and the cat command is that when we use the cat command, it will be possible to enter the text in the new file. Immediately while with the touch command this will not be possible.

One of the advantages of the touch command is that it allows us to create multiple new files with a single command simultaneously.

  1. To create a new file using touch we will execute the following:
    touch file.txt
  2. To confirm that our file has been created correctly, we will run the following:
    ls -l file.txt
  3. As we have indicated, the touch command gives us the opportunity to create multiple new files at once, for this, it is enough to add all the additional filenames (separated by spaces) as necessary:
    touch File.txt File.txt File.txt
  4. To add content to this file, we can use a text editor such as nano or vi:
    nano file.txt

Create a Text File using the Redirect Symbol in Linux

Another alternative to creating a text file in Linux is using the standard redirection symbol which is usually used to redirect the output of a command to a new file.

  1. To create a new file, we will execute the following:
    > file.txt
  2. To see that our file has been created we will run the following:
    ls -l file.txt

Thus, we have seen various ways to create our text files in Linux in a practical way, with security and ease to enter all the information we consider necessary for those files.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *