Create file linux

Table of Contents


To create a file in Linux you can use different ways, in particular in this post we will see how to do it using the touch command or the nano and vim editor.

Don’t worry if you don’t know these tools, in each section you will find simple explanations full of examples.
The nice thing is that with Linux you can create files of any extension directly from the command line without needing any additional tools, isn’t that great?!

How to create a file using linux command line

In this section we will see how easy it is to create a file using the Linux command line.

There are several ways to achieve this but the most useful from my point of view are the touch command and the nano and vim editor.
In the next sections I will explain how to do it and we will see some examples to understand better.

Create file using touch linux command

To create a new file using the linux command line you can use the touch command.

Actually this command was born to update the access and modification times of each file to the current time, but if the file doesn’t exist, it is created.
You can use the touch command as follows:

touch [options] <filename>

However, to create a file, you don’t need to add any options, just put the name of the file you want to create.

touch a_new_file.txt

If you try to run this command in your local environment and you don’t have a file called a_new_file.txt, you will see that a new will be created.

Please note that using the touch command, the new file will be created empty. You can use an editor to fill the content of the file.

Create file using nano linux command

You can create a file using a Linux editor directly using the nano command.
Like all Linux commands, nano can be used with various parameters.

nano [options] <filename>

However, to create a file you need just to specify the filename.

nano another_file.txt

By typing the command shown above, a screen will open where you can edit your file.

This, unlike the Linux touch command, allows you to edit the file directly.
Keep in mind that if the file already exists you can edit it instead of creating it.

When you have finished your changes you can save using the key combination Ctrl + O and exit with Ctrl + X.

The Linux command nano also provides other shortcuts which are all visible at the bottom of the page when you edit the file.

Create file using vi linux command

Another way to create a file using the Linux command line is the vi command.
This is another nano-like editor that we saw in the previous section.

vi [options] <filenme>

As always, you can add various options to the vi command, but the name is enough to create a file.

vi third_file

As with nano, once you type this command you will get a window where you can edit your file.
To start typing with vim you need to press i.
When you're done press Esc + : + x + Enter`.
This will save the file and its contents.

Create file using cat linux command

Usually the Linux cat command is used to display the contents of a file.
However, you can also use it to write a new file.
The cat command is structured as follows:

cat [options] <filenme>

To create a file with the Linux cat command, just use this command:

cat > <filename>

At this point you will be given the possibility to write the contents of the file directly from the terminal.
If you need to write on multiple lines just press ENTER.

When finished press Ctrl + d to save and exit.

Create file using echo and printf linux command

Last method but not least is the use of the echo command.

echo "hello world" > my_file.txt

In this example we are going to write the string hello world to our file called my_file.txt.

Alternatively, you can also use the Linux printf command.
the mechanism is exactly the same.

printf "hello world" > my_file.txt

One important tip I want to give you is that if you want to create a new file you have to use > otherwise, if the file already exists and you want to append the content, use >>.


As you have seen creating a file in Linux is really simple and there are multiple ways to do it.
In this tutorial we have seen how to use the touch, nano, vi, cat and echo/printf command.
I hope everything is clear to you, but if you have any doubts, do not hesitate to contact me or write a comment in the section below.

If everything is clear, move on to the next article!

Leave a Comment

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