Skip to content
Step by Step Internet 馃寪 Guides for learning to surf the Net

How to create files in Linux easily and without errors in any distribution? Step by step guide

It is a fact that Linux is preferred by many users due to its ability to control virtually your entire system via command lines, to the point where experienced users prefer this option to a regular desktop environment.

One of the main functions of this terminal interface is to facilitate the management of files, archives and directories that you have on your computer: create them, delete them and move between them.

In the next paragraphs we will explain the better ways to manage your files, create new files and you will also know which are the best commands for managing directories and files.

What are Linux files and what are all the types that exist?

File converter to Ext 4 on Linux

Linux supports a wide variety of file formats, both disk-based, and .ext2, .ext3, .JFS and .UFS, as well as those used for communication with other systems on the network, as is the case of NFS.

The most popular and used file systems are:

  • Ext2: it is the enhanced version of the ext system. It is compatible with large file systems, being able to handle files of up to 2Gb with very little fragmentation,
  • Ext3: Considered the standard system in the GNU / Linux community, it receives constant maintenance so it is highly reliable and is compatible with ext2.
  • Ext4: It is the latest version of the ext family. It is much more efficient than its predecessors and its size limit has been extended to 16Tb, and it can handle 1024 PB file systems.
  • ReiserFS: It represents the latest generation of file systems for Linux, since it optimizes operations with them, organizing the material in a more efficient way.
  • Swap: this is the file system defined for the Linux swap partition, which is necessary to avoid saturating your computer’s RAM memory when it reaches its limit.

What are the main commands to create files and archives on a Linux computer?

Touch command statement on Linux

The creation of files in Linux is predominantly done through the command terminal and, For this task we have a variety of commands of different complexity that will allow you to create the “.txt” files in Linux.

Let’s see:

  • Cat: it is a very popular command in the Linux community thanks to its usefulness. It allows you to create, combine and print files, among many other functions.
  • Touch: its main use is the creation of empty files and the modification of the information of time frames of files or directories.
  • Threw out: This command has several purposes. It is useful for writing formatted text in the Linux terminal, as well as creating text and log files.
  • Printf: is specialized for printing formatted data. The “printf” command gives us tools to take data entered in the file and format it.
  • Heredoc: It is not a command itself, but rather a redirection that allows you to enter multiple lines of input, working as a complement to a command.

Learn step by step how to create a file in the Linux console correctly like an expert

Linux has a wide variety of options for creating and managing files, so now we will explain how to create one with several available methods, so you can manage your directories like a professional.

Go for it:

With the cat command

Cat command statement on Linux

The first step is to open the Linux command terminal and enter the following command:

  • cat > archivo.txt

After pressing “Enter”, the cursor will move to the next line for you to start entering the text you want. You can continue adding all the necessary text by pressing “Enter” at the end of each line. Once everything is in order, press the keys together “CTRL + D”. This will close the file and take you back to the message you created.

If you want to review the details of the file, insert this command:

  • ls -l archivo.txt

He will show you details like date and time in which created the file, and the assigned permissions of it.

With the touch command

Unlike the cat command, touch does not allow immediate text editing of the file. Instead, it offers us the advantage of being able to create several files at the same time by entering a single command.

To start creating the file, open the command terminal and type:

  • touch archivo.txt

This command will create an empty file that we must fill using a text editor such as nano or vi using the following command:

  • nano archivo.txt

Once executed, will allow us to edit the text of the file.

If you want to create more than one file using the same command line, all you have to do is enter the names of the other files you want to create, separated by a space, like this:

  • touch Archivo.txt Archivo.txt Archivo.txt

Add all the names what do you need for create the number of files that you require.

If you want to verify that your file was created correctly, the command you must enter in the terminal is this:

  • ls -l archivo.txt

With the redirect symbol in Linux

Linux system symbol

A native Linux option for the file creation is to use the Linux redirect symbol, although this is usually used to redirect the information output of the command to an existing file.

The command to create a file by this means is the following:

  • > archivo.txt

Entering this command will create a blank file, and to verify that it was created correctly enter:

  • ls -l archivo.txt

Which will show you the specifications and the file creation data. Since the created file is blank, you will need to use a text editing tool to enter the content.

To do this, you can enter the command with the name of the editor, followed by the name of the file, in the terminal, like this:

  • vi archivo.txt

With Echo

The Echo command works in a particular way, since the text that we want the file to contain must be entered in the command line, so that, when executing it, the text will be copied into the created file.

The echo command to create a file looks like this:

  • echo ‘contenido del archivo’ > nombrearchivo.txt”.

After which, you must verify that the file is correctly made by means of the following command:

  • ls

Since Echo does not have a review command, we will use the cat command to be able to view the contents of the file:

  • cat nombrearchivo.txt

With Printf

Printf command statement on Linux

The Printf command works in a similar way to the echo command when creating a files, that is, the text of the file is included in the command line before being executed, but in the case of Printf, only the first line of text is entered.

As follows:

  • printf ‘Primera línea de texton’ nombrearchivo.txt

Command line change if you want to enter the first two lines of text.

In this case, both are separated by ” n”, and the file name will end in “2”:

  • printf ‘Primera línea de texton Segunda línea de texto’ nombrearchivo2.txt

Another similarity to the echo command, is the need to resort to another read command to view the content of the file.

Depending on the lines of text, we will use one of the following commands to check the content of our files:

  • cat nombrearchivo.txt
  • cat nombrearchivo2.txt