Introduction to Linux Create File
In Linux Operating System creating a file is an important task since the user or administrator may be required to create a file multiple times in a single day. A file can be created either using the command line or by using the file manager. In this article, we will discuss multiple ways of creating a file. Commands like touch, echo cat and print and text editors like nano, vim and vi are used to create a new file.
How to Create a File?
Before creating a file you should check whether you have to write permissions in the parent directory. If you don’t have write permission then you will receive permission denied error. Use the ls command to view the data of the directory.
1. Using Touch Command
The touch command is one of the easiest ways to create a file in the Linux shell. To create a new file just type the touch command followed by the name of the file with the extension
$ touch filename.filetype
$ touch touch.txt
$ touch touch.docx
If the file doesn’t exist, then the touch command will create one and if the file already exists then the command will modify its timestamps. While using this command we cannot write any data while creating a new file. By default the size of the newly created file is 0KB.
To view the newly created file using the ls command.
$ ls -l
To create multiple files simultaneously enter the file names and separate them with spaces.
Example: $ touch file1.docx file2.docx file3.docx
2. Creating File with Redirection Operator
The redirect symbol (>) is also used to create a new file. As the name suggests the redirection operator pipes the output of the command to another command or file. To create a file using this operator, just type the file name after the redirection operator. Even in this method, you cannot enter the text directly. Unlike touch command, you can create only one file at a time. This is the shortest method to create a file in Linux OS.
$ > test2.txt
If the file name already exists it will overwrite the file, otherwise the new file will be created.
3. Using Cat Command to Create File
We can also use the cat command to create a file. One important advantage of this method is that you can start adding text to your file immediately. To create a new file just type the cat command followed by the redirection operator and then the filename and then press the Enter key.
Then you can enter the text immediately to your file. After completing, press Ctrl + D to save the file.
$ cat > cat.txt
To view the file type the command below
$ ls -l cat.txt
4. Using Echo Command to Create File
This command prints the text that is entered as parameters to the command which is then redirected to the file. To create a file type the echo command, then type the text you want to display and then use the redirect symbol to write the contents to the file that you desire to create.
Use the following command line to create a file using echo command.
$ echo “ Linux is simple and easy to learn” > echosample.txt
To create only an empty file use the following command.
$ echo > echosample.txt
How to Create File Using Herdoc?
Heredoc or Here document refers to a kind of redirection that allows a user to pass multiple lines to a particular command. This method is used when you need to create a file that contains multiple lines.
Use the following command to create a file using heredoc
$ << EOF > sample.txt
1. Using DD Command
Sometimes we need to create large files for testing the write speed or testing the network speed. dd command is used when there is a need to create large files.
Example: To create a file of size 1 GB run the following command.
$ dd if=/dev/zero of=sample.test bs=1 count=0 seek=sample
Here sample.test is the file name.
2. Create File with fallocate Command
fallocate is a utility command which allocates real disk space for a file.
$ fallocate -l 1G sample.test
The above command will create a file named sample.test of size 1 GB.
3. Create File With Print Command
This command is similar to the echo command and it provides some formatting functions. To create a file containing a single text line, use the following command.
$ printf 'first line\n' sample.txt
To add two lines, use the \n option to separate each line.
$ printf 'first line\n second line' sample.txt
Now let us see how to create a new file using text editors.
- VI Text Editor
This is the oldest of all text editors in Linux system. It is used for editing text files directly.
To create a new file using Vi text editor, type the following:
Your screen will change to text editor mode. Press the I key to change to insert mode. Now type the contents of your file. To save the file and exit press the button Esc:x and press Enter key.
- VIM Text Editor
You may see that the Vi editor is not so user-friendly. Vim is a modified version of the Vi editor. Vim stands for Vi modified.
Use the following command to create a new file.
The screen will look like the Vi editor screen. Press I key to type the text and after typing, save the file and exit by typing the following.
Esc :wq Enter
- Using Nano Editor to Create New File
Nano editor is the latest and much easier editor of three text editors that we have seen so far.
To create a new file, type the following command.
$ nano test9.txt
On typing the above command you will be directly taken to the editing mode. It will also display a list of useful commands at the bottom of your screen. Enter the text and press Ctrl + O to save the file. To exit the nano editor press Ctrl+X.
This is a guide to Linux Create File. Here we discuss the different methods of creating a new file in the Linux Operating System and its Examples along with its Code Implementation. You can also go through our other suggested articles to learn more –