Discover the Best 7 Ways to Create a File in Linux Terminal

linux create file
In this article, I will show you how to create a file from a Linux terminal. There are many text editors like (vim, nano, vi) and many commands like (cat, echo, printf, touch) to create a file in the Linux operating system via command line. This article will contain on the following tools:

  1. Touch command
  2. Cat command
  3. Echo command
  4. Printf command
  5. Nano text editor
  6. Vi text editor
  7. Vim text editor

1) Create a file with touch command

we will use touch command with any extension to create file, this command will create an empty file touch.txt in your current directory as an example below.

$ 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

2) Create a file with cat command

We will use cat command to create file, this command will create an empty file cat.txt in your current directory as an example below, but you must add text in the file.

$ 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

3) Create a file with echo command

We will use echo command to create file, this command will create a file echo.txt in your current directory as an example below, but you should add text in the line command.

$ 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

4) Create a file with printf command

We will use printf command to create file, this command will create a file printf.txt in your current directory as an example below, but you should add text in the line command.

$ 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 nano text editor

To create a file using nano text editor, first install it, after that type command below and the text editor will be opened to adding text.

$ 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

6) Create a file with vi text editor

To create a file using vi text editor, type command below and the text editor will open the file, but you can't add any text before converting it to insert mode by typing i character.

$ 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

7) Create a file with vim text editor

To create a file using vim text editor, type command below and the text editor will open the file, but you can't add any text before converting it to insert mode by typing i character.

$ 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

Thanks for reading the article and please leave your comments.

Read Also:

Ahmed Abdalhamid 8:25 am

About Ahmed Abdalhamid

Technical Support Engineer experienced in Linux servers administration of production environments. Exploring DevOps culture and tools. Interested in containerization and open source monitoring tools.

Author Archive Page

Have anything to say?

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.

5 Comments