How To Create a File in Linux


In operating systems, files are the building blocks that contain data, configurations, and programs. They aid in system customization, data organization, scripting and programming, user collaboration, and more. 

There are various types of files, including text, binary executable, media, system, and many more. Despite this variety, text files contribute roughly 50 to 80% of the data. Hence, it is a crucial aspect that users must be aware of. Many beginners do not yet know the approaches to making new text files. So, in this guide, we briefly describe how you can create a file in Linux without any hassles.

There are three simple methods for creating a file in Linux: a text editor, the touch command, and the redirection operator. Let’s divide this section to discuss each in detail using suitable examples.

The Text Editors

Linux’s text editors are powerful yet simple tools to create text files and edit them. There are various text editors like Nano and Vim in Linux systems. For instance, to create a file, say sample.txt using nano, your command would be:


On entering this command, it will create the sample.txt file and open it in the text editor window.


Similarly, you can use the following command to create the text files through the vi text editor: 

vi sample.txtspan style=“font-weight: 400”> span>


The touch Command

The touch is used to create the empty files and update a file’s timestamps quickly. To use it for creating a text file, use the following command:


Redirect Operator

Suppose you execute a command or script whose output you want to save in a text file. This is where the redirect operator “>” comes into play. For example, to save the output of an echo command in a new text file, “sample_file.txt,” you should use:

echo “Hello, this is a sample file.” > sample_file.txt


A Quick Wrap-up

Knowing how to create a file is a must-have skill for every Linux user. This guide lists three approaches that you can use to make a text file. These methods involve using a text editor, the redirect operator, and the touch command. While the way through the text editor is simplest, the other two methods have their use.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More