In this article, we will learn how to create a new file in Linux on a regular basis. You can create a new file either from the command line or from the desktop file manager.
What you’ll learn
- How to create a file in Linux
- Some basic about nano
What you’ll need
- Ubuntu Desktop
- Sudo-privileged user account
- Basic Linux command line knowledge
Create a File in Linux
There are various ways to quickly create a new file in Linux using the command line.
- Using touch Command
- Using cat Command
- Using echo Command
- Using text editor
Creating a File with touch Command
The touch command allows us to update the timestamps on existing files and directories as well as create new, empty files.
To create a new file simply run the touch command followed by the name of the file you want to create:
$ touch file1.txt
To create multiple files at once, specify the file names separated by space:
$ touch file1.txt file2.txt file3.txt
Creating a File with cat Command
The cat command is mainly used to read and concatenate files, but it can also be used for creating new files.
To create a new file run the cat command followed by the redirection operator > and the name of the file you want to create. Press Enter type the text and once you are done press the CRTL+D to save the files.
$ cat > file1.txt
Creating a File with echo Command
The echo command prints the strings that are passed as arguments to the standard output, which can be redirected to a file.
To create a new file run the echo command followed by the text you want to print and use the redirection operator > to write the output to the file you want to create.
$ echo "Some line" > file1.txt
If you want to create an empty simply use the:
$ echo > file1.txt
Creating a File using a text editor
There are many text editors where you can create and edit the file. In this tutorial, we are using the Nano text editor.
$ nano [file name]
This is the way how to create a new file in Linux on a regular basis using the command line and you can use any text editor.
You can watch this video on how to create a new file in Linux on a regular basis.
In this tutorial, you learned how to create a new file in Linux from the command line using various commands and a text editor.
If the command line is not your thing you can easily create a blank text file using the right-click menu in the File Manager.
If you have questions, feel free to leave a comment below.