Best Ways to Create a File in Linux

Best Ways to Create a File in Linux
Best Ways to Create a File in Linux

Create a File in Linux

There are various ways to create a file in Linux. You can create files using different commands through the terminal and edit it. In this tutorial, you are going to learn Best Ways to Create a File in Linux.

1. Create a File Using touch command

You can create a file using touch command. To create a file using touch command run below command. It will create demo.txt file in the current directory:

touch demo.txt

Confirm the file created by running below command:

ls -l

The output should be:

-rw-r--r--. 1 root root       0 Apr  11 21:12 demo.txt

2. Create a File Using nano command

You can create a file using nano command. To create a file using nano command run below command. It will create demo2.txt file in the current directory:

nano demo2.txt

Now write something in the file and press Ctrl+x the y to save file.

Confirm the file created by running below command:

ls -l

The output should be:

-rw-r--r--. 1 root root       0 Apr  11 21:16 demo2.txt

3. Using echo command

You can create a file using echo command. To create a file using echo command run below command. It will create demo3.txt file in the current directory:

echo "Hello World" > demo3.txt

Confirm the file created and check file contents by running below command:

cat demo3.txt

The output should be:

Hello World

4. Create a File in Linux Using vi command

You can create a file using vi command. To create a file using vi command run below command. It will create demo4.txt file in the current directory:

vi demo4.txt

Now write something in the file and press esc(Escape Key) then :wq to save file.

Confirm the file created and check file contents by running below command:

cat demo4.txt

The output should be:

Hello World

5. Using printf command

You can create a file using printf command. To create a file using printf command run below command. It will create demo5.txt file in the current directory:

printf "Hello World" > demo5.txt

Confirm the file creation and check file contents by running below command:

cat demo5.txt

The output should be:

Hello World

Conclusion

In this tutorial, you have learned Best Ways to Create a File in Linux successfully. If you have any of the queries regarding this then you can comment below.

LEAVE A REPLY

Please enter your comment!
Please enter your name here