A file on a computer stores data, information, settings, etc. On a Linux system, you can create a file in so many ways for instance if you are using GUI then programs like a GUI text editor, Libre office can be used.
Another way of creating a file in Linux is by using core commands or command line text editors such as vi, nano, etc. In this article, we will discuss creating files using command and command-line text editors.
How to create files using the touch command
By using the touch command you can easily create a text file from your terminal. To create a file testfile.txt use the following command in your terminal –
If a file already exists with this name in your current working directory then it will update the access and modification time of that file.
You can create multiple files by using the following command in your terminal –
touch testfile1.txt testfile2.txt testfile3.txt
This will create three files in your current working directory.[alert color= yellow]Note: At some places in your filesystem such as inside
/root directory you may need to have root privileges so you should use the
sudo with your command [/alert]
Creating a file using a redirection operator
You can use two types of redirection operators i.e.
>> to create a file. If you use
> it will create a new file if the file already exists in your current working directory the content of that file will be overwritten. Using
>> will append the newly added content to the end of the file.
Now use the following command to create a text file using redirection operator –
This is one of the shortest and easiest commands to create a file in Linux.
How to create a file using cat command
In Linux or Unix cat command is used to concatenate the files to standard output. We can use it with the redirection operator to create a new file in our system. Using
> will overwrite the content of the file while
>> will append the content to this file.
cat > testfile.txt
Once you execute this command immediately text can be added to this file. After entering the text, press
ctrl+d to save the content.
To display the content of the given file use –
How to create a file using the echo command
The echo command print a string passes it as an argument to the standard output. We can use it with a redirection operator to create a new file simultaneously we can insert some content into this.
To create an empty file using the echo command use the following command in your terminal –
echo > testfile.txt
How to create files using a command-line text editor
There are various command-line line text editors that can be used to create, modify a file in Linux. Some of them mostly used text editors are vim, nano, ee, etc.
To create a file using nano editor open your terminal and use –
Once you execute the above command file will open to enter the text,
ctrl+s to save the content of the file then
ctrl+x to exit from the editor.
To create a file using vi or vim editor use the following command in your terminal –
The file will be open in the command mode you can start entering text by pressing
i to save the file press
Esc and then
Now you know several options to create a file in Linux. If you have a query you can leave it in the comments below.