How to create a file in the Linux terminal?
 

How to create a file in the Linux terminal?

 

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 –

touch testfile.txt

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.

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

Creating a file using a redirection operator

You can use two types of redirection operators i.e. > and >> 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 –

> testfile.txt

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 –

cat testfile.txt

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 –

nano testfile.txt

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 –

vi testfile.txt

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 :wq.

Conclusion

Now you know several options to create a file in Linux. If you have a query you can leave it in the comments below.

 

Previous My mobo died :(
Next [ubuntu] USB Harddrive versus USB keyboard.
 

About author

You might also like

Linux Terminal 0 Comments

How To Use Lynx For Web Browsing Through Terminal in Linux?

This post was last updated on August 1st, 2020 at 09:31 pmLynx is a text-based web browser that is used to browse the internet through Linux Terminal. It displays the

Linux Topics 0 Comments

How To Install And Work With Nano Text Editor In Linux?

This post was last updated on August 1st, 2020 at 10:12 amNano is a simple and user-friendly text editor mostly used on Unix like operating systems. It emulates the Pico

Commands 0 Comments

Using mkdir command to make a directory in Linux

There are different ways to create a directory (folder) in your Linux system. If you are using a desktop environment then you can use the file manager to create or

0 Comments

No Comments Yet!

You can be first to comment this post!

Leave a Reply