File management is the key operation in any operating system. In Linux, a file is a container for storing information, and creating a file is a basic task. Linux-based Operating Systems offer the use of a terminal and the GUI to create and manage files. This post will demonstrate the possible methods to create a file in Linux. The content of the post is given below:
- How to Create a File in Linux?
- Using touch Command
- Using Redirect Operator
- Using cat Command
- Using echo Command
- Using printf Command
- Using Vim | A Text Editor
- Using Nano | A Text Editor
Let’s start the guide!
How to Create a File in Linux?
In Linux, the terminal is the most effective component in a Linux-based system. All the tasks are performed using the terminal. Here, a set of commands is demonstrated that can be used to create a file on Linux. Let’s start:
Using Touch Comand
The simplest way to create a file in Linux is using the “touch” command. To create a file named “test1.txt”, we used the below-provided command. Furtherly, The “ls” command is used to verify the newly created file:
|$ touch test1.txt $ ls|
Using Redirect Operator
The redirector operator navigates the system to that file location, and if the file does not exists, it creates the file. To create a “test2.txt” file utilizing the redirect operator “>”, we executed the following command in the terminal:
|$ > test2.txt $ ls|
Note: The redirect operator can also be used with the “cat”, “echo”, and “printf” commands to create a file. A practical demonstration of such scenarios is discussed in the upcoming part of the guide.
Using cat Command
The main usage of the “cat” command is to concatenate the file’s content to the output and allow writing the text in the file. But using the “redirector operator” with the cat command allows you to create a file.
As an example, the following command will create a “test.txt” file using the “cat” command:
|$ cat > test3.txt|
Using echo Command
The “echo” command is commonly used for displaying the text. However, using the redirect operator “>” with “echo” creates a new file if not exist. For instance, the command provided below will create a file named “test4.txt” and is verified using the “ls” command:
|$ echo > test4.txt $ ls|
Note: The “echo” command can also be used to set the content and create a new file that contains the content. The following example is practiced to do so, which adds content and creates a new file named “test5.txt”:
|$ echo ‘Sample text’ > test5.txt $ ls|
Using printf Command
Prinff command is used for formatting the text and printing that formatted text. To create a new file “test5.txt” and set its contents, use the below command utility:
|$ printf ‘Line one sample text\n’ > test5.txt $ ls|
Using Vim | A Text Editor
Vim is popular text editing software used for Linux distros. Vim is a modified version of Vi text editor, We can create a new file using Vim as follows:
|$ vim test6.txt|
After executing the command, Vim/Vi will take you to the editor as shown below. Press “i” to insert the text (in my case, “sample text”), then press the “Esc” button. Write the “:wq” command and press “Enter” to exit the editor.
Using Nano Text Editor
To create a file using the Nano editor, you can follow the below steps:
Open the Nano editor and run the nano command with a filename. For creating a new file “testfile.txt”, use the nano editor as follows:
|$ nano testfile.txt|
The nano editor is opened, write your data in the editor (optional) as shown below:
Hold the “Ctrl” key and press “O”, give the file a specified name, and press the “Enter” key to save the file.
Bonus Tip: How to Create a File of a Specific Size?
If you have a large file and want to keep every file to a specific size, use the “fallocate” command. The below command is creating a file “testVideo.mp4” file the size “1G” as 1GB:
|$ fallocate -l 1G testVideo.mp4|
That’s the end of the guide!
To create a file in Linux, use the “touch” command and “cat”, “echo”, and “printf” commands with the redirect operator. Vim and the nano editor can also be used to create a file in Linux. Moreover, a file of a specific size is created using the “fallocate” command. In this post, all the possible commands to create a file in Linux are demonstrated.