7 Ways to Create a File through Linux Terminal

7 Ways to Create a File through Linux Terminal

In this article, I will show you how to create a file through Linux terminal.

1. Create a file with echo command

We will use echo command to create file, echo.txt file created in your current directory.

$ echo "This file has been created with echo command" > echo.txt

To see the file,type command below.

$ ls -l echo.txt
output
-rw-r--r--. 1 root root       0 Sep  4 08:06 echo.txt

To open the file, we will use cat command to open it.

$ cat echo.txt
output
This file has been created with echo command

2. Create a file with touch command

We will use touch command to create file, touch.txt file created in your current directory.

$ sudo touch touch.txt
$ sudo touch touch.docx

To see the file type command below.

$ ls -l
output
-rw-r--r--. 1 root root       0 Sep  4 08:08 touch.docx
-rw-r--r--. 1 root root       0 Sep  4 08:06 touch.txt

3. Create a file with cat command

We will use cat command to create file, cat.txt file created in your current directory.

$ cat > cat.txt

Add the text below.

This file has been created with cat command

To save the file hit Ctrl + d, and to see the file type command below.

$ ls -l cat.txt
output
-rw-r--r--. 1 root root       0 Sep  4 08:06 cat.txt

To open the file, we will use cat command to open it.

$ cat cat.txt
output
This file has been created with echo command

4. Create a file with printf command

We will use printf command to create file, printf.txt file created in your current directory.

$ printf "This file has been created with printf command" > printf.txt

To see the file type command below.

$ ls -l printf.txt
output
-rw-r--r--. 1 root root       0 Sep  4 08:06 printf.txt

To open the file, we will use cat command to open it.

$ cat printf.txt
output
This file has been created with printf command

5. Create a file with vi text editor

Type command below and the text editor will open the file, and type i for insert mode.

$ vi vi.txt

Add the text below.

This file has been created with vi text editor

To save the file and exit hit Esc after that :wq, To see the file type command below.

$ ls -l vi.txt
output
-rw-r--r--. 1 root root       0 Sep  4 08:06 vi.txt

To open the file, we will use vi command to open it.

$ vi vi.txt
output
This file has been created with vi command

6. Create a file with nano text editor

We will use nano command to create file, nano.txt file created in your current directory.

$ nano nano.txt

Add the text below.

This file has been created with nano text editor

To save the file type Ctrl + x and type y, to see the file type command below.

$ ls -l nano.txt
output
-rw-r--r--. 1 root root       0 Sep  4 08:06 nano.txt

To open the file, We will use nano command to open it.

$ nano nano.txt
output
This file has been created with nano command

7. Create a file with vim text editor

Type command below and the text editor will open the file, and type i for insert mode.

$ vim vim.txt

Add the text below.

This file has been created with vim text editor

To save the file and exit hit Esc after that :wq, to see the file type command below.

$ ls -l vim.txt
output
-rw-r--r--. 1 root root       0 Sep  4 08:06 vim.txt

To open the file, we will use vim command to open it.

$ vim vim.txt
output
This file has been created with vim command

Conclusion

We learned the different ways to create a file through Linux terminal. Hope you enjoyed reading.

2 Comments

Leave a Reply