Before creating a file, ensure that you have the required access to create the file in the parent directory; otherwise, you will get the permission denied error on the screen.
For checking the files that are available within the directory, use the “ls” command. We will learn about various commands for creating files.
Creating New Files From the Command Prompt
Using a command-line interface, you can easily and efficiently create a file on the Linux system that does not exist already. Also, there is no need to open any application to create the file, as you can create it directly using the command line.
Using the touch Command
The “touch” command is one of the easiest and commonly used commands in a Linux system that will allow you to create a new file on a Linux system. Run the command below to create a new file.
With this command, you will be able to create an empty file and check the newly created file with the help of the “ls” command, as shown below.
The “ls” command will help show all the contents of the present working directory, and you can see the newly created files there, as shown below.
If the newly created file name already exists in the directory, it will create the new file with the new timestamp differentiating it from the older one.
Using Redirect Operator
Consider the redirection operator as a character that changes the destination and displays the results.
Using the right angle bracket “>”, the system will output the result into the file you specify. The file name is usually the target. This symbol allows the creation of a new file, as shown below.
This command will create a new and empty file.
If you want to check the newly created file “test2” status, use the “ls” command as shown below.
Creating File Using cat Command
The “cat” command stands for the term concatenation. It will help provide the output of several files, a single file, or a part of the file to the designated file. The “cat” command will create the file if the file does not exist, as shown below.
This command displays the contents of the test2.txt file on the screen. But after specifying the redirection operator, it will place the content in the test2.txt file, as shown below.
To verify the created file, you can run the “ls” command as shown below.
Creating File Using the echo Command
As shown below, the echo command is used to duplicate the content that we will specify and copy it to the file we specify.
You can check the newly created file using the “ls” command, as shown below.
The above image confirms that the test4.txt file is added to the list. If you wish to display the content of the test4.txt, rerun the cat command for the test4.txt file as shown below:
The command mentioned above will display the content of the file on the screen.
Creating File Using the printf Command
The “printf” command works almost the same as the echo command; along with this, it will also allow you to add some formatting functionality, as shown below in the example.
The above command will specify the “\n”, allowing you to enter the new line.
Later, you can use the “cat” command to list the contents of both files.
Creating Files Using Text Editors
Each Linux distribution comes with a text editor or multiple editors to create files and important tasks. Here we are focusing on the most commonly used text editors for creating new files in Linux.
Using Vi Text Editor
It is the oldest and most commonly used text editor among various alternatives. This tool helps in creating or editing the existing files. Use the below-mentioned command for using the Vi.
Once you run the command, another screen would appear, which is the Vi screen. To insert the data, type “-i” to enter the insert mode. Then start typing. Once you are done, you can press Esc “x and hit enter to exit the Vi mode.
Using Vim Text Editor
Some users may not find Vi text editor very friendly; thus, Linux comes with a newer version called Vim that stands for Vi editor, Modified.
You can create the new file using Vim, as shown below.
The screen will look similar to the Vi editor, and to enter the insert mode, you can press “i” and type some words. Now you can save and exit the file using the below command.
Using Nano Text Editor
It is a new text editor with a user-friendly interface. It is creating a new file using the below-mentioned command.
You will get into the insert mode and useful commands shown at the bottom to ease work by default.
Once you enter the desired text, you can press “ctrl+l+o” to save the changes. For exiting the editor, you can press “ctrl+l+x”.
Creating files is one of the common tasks that users carry out. You can easily create a new file in Linux using various utilities like various commands and text editors. We have mentioned various commands like cat, Echo, redirect operator, and touch commands to create a new file. These commands are not difficult to remember, and you can choose any of them as per your requirement. Also, you can use the text editors of your choice to create and edit files easily.