How to Create File in Linux

Introduction

In this lesson, we will see how to create files in Linux system. Linux is an operating system known for its robust command line interface. Creating files in Linux can be done in several ways. In this tutorial, we will explore different methods of creating files in Linux. By the end of this article, you will have a clear understanding of how to create files in Linux.

Create a file in Linux

Method 1: Using the touch Command

The touch command is the simplest and most common way to create a file in Linux. The command creates an empty file with a specified name. To create a file using the touch command, open a terminal and navigate to the directory where you want to create the file. Once you are in the directory, use the following command:

$ touch filename

Replace “filename” with the name you want to give to the file. For example, to create a file named “my_file.txt,” use the following command:

touch my_file.txt

The touch command will create an empty file named “my_file.txt” in the current directory.

Create multiple files using the touch command
$ touch info.yaml data.txt mail.tmp

Method 2: Create a file using the Redirection operator “>”

$ > info.yaml

Method 3: Using the echo Command

The echo command is another way to create files in Linux. The command is used to print text on the screen. However, you can use the echo command to create a file by redirecting the output to a file. To create a file using the echo command, open a terminal and navigate to the directory where you want to create the file. Once you are in the directory, use the following command:

echo "content of the file" > filename

Replace “content of the file” with the text you want to include in the file and “filename” with the name you want to give to the file. For example, to create a file named “my_file.txt” with the contents “Hello World,” use the following command:

echo "Hello World" > my_file.txt

The echo command will create a file named “my_file.txt” with the contents “Hello World” in the current directory.

Create an Empty file with echo command

$ echo > map.yaml

Method 4: Using the cat Command

The cat command is another way to create files in Linux. The command is used to concatenate files and display the output on the screen. However, you can use the cat command to create a file by redirecting the output to a file. To create a file using the cat command, open a terminal and navigate to the directory where you want to create the file. Once you are in the directory, use the following command:

cat > filename

Replace “filename” with the name you want to give to the file. For example, to create a file named “my_file.txt,” use the following command:

cat > my_file.txt

After entering the command, press Enter, and you will see a blank line waiting for input. Type the contents of the file and press Ctrl + D to save the file and exit the cat command. The contents you typed will be saved to the file.

Method 5: Create a file using Heredoc

Heredoc allows us to redirect multiple lines to a file

$ cat << EOF > info.yaml

This is the first line
This is the second line

This is the third line

EOF

Method 6: Using a Text Editor

Another way to create files in Linux is by using a text editor. Text editors are applications that allow you to create and edit text files. Some popular text editors in Linux include Nano, Vim, and Emacs. To create a file using a text editor, open the text editor of your choice and create a new file. Once you have created the file, save it with a name and in a location of your choice.

For example, to create a file named “my_file.txt” using Nano, open a terminal and navigate to the directory where you want to create the file. Once you are in the directory, use the following command to open Nano:





nano my_file.txt
# create a file using vi/vim
# vi my_file2.txt

This command will open the Nano editor and create a new file named “my_file.txt.” You can

Method 7: Using the dd Command

The dd command is another way to create files in Linux. The command is used to convert and copy files. However, you can use the dd command to create a file by specifying the file size. To create a file using the dd command, open a terminal and navigate to the directory where you want to create the file. Once you are in the directory, use the following command:

dd if=/dev/zero of=filename count=1 bs=file_size

Replace “filename” with the name you want to give to the file and “file_size” with the size of the file you want to create. For example, to create a file named “my_file.txt” with a size of 1MB, use the following command:

dd if=/dev/zero of=my_file.txt count=1 bs=1M

The dd command will create a file named “my_file.txt” with a size of 1MB in the current directory.

Edit the File

After creating the file, you may want to edit its contents. To edit the file, you can use any text editor that is available on your system. For example, to edit the file using the nano editor, you can type the following command:





nano my_file.txt

This command will open the “my_file.txt” file in the nano editor. You can then add or modify the contents of the file as per your requirements.

Save the File

Once you have edited the file, you need to save the changes. To save the file in the nano editor, you can press “Ctrl+O” and then press “Enter” to confirm the filename. You can then exit the editor by pressing “Ctrl+X

Also, see file manipulation and how to Find file in Linux . See complete examples in our GitHub repository.

Follow us on social media

Author