7 Ways to Create a File in Linux Terminal
Abstract: we will use cat command to open it. $ cat printf.txt outputwe will use cat command to open it. $ cat echo.txt output
In this tutorial, 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. Here will explain the following linux tools.
- Touch command
- Cat command
- Echo command
- Printf command
- Nano text editor
- Vi text editor
- Vim text editor
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
Conclusion
In this tutorial, we learned the different ways to create a file from Linux terminal. Hope you enjoyed reading and please leave your comments in the below comment section.
Read Also:- Linux touch command - Why do we need to change Timestamp?
- 20 Linux Cat Command Examples For File Management
- How to Install Atom Text Editor in Linux
- Suplemon - Modern CLI Text Editor with Multi Cursor Support
- AMP - Fully Featured Text Editor for Linux Terminal