How to Create a File in the Linux Using the Terminal?

09 Apr, 2021
In this article, we will learn to create a file in the Linux/Unix system using the terminal. In the Linux/Unix system, there are the following ways available to creating files. 
Using the touch commandUsing the cat commandUsing redirection operatorUsing the echo commandUsing the heredocUsing the dd command
Using the touch command
Using the cat command
Using redirection operator
Using the echo command
Using the heredoc
Using the dd command
The touch command is used to create file/files without any content and update the access date or modification date of a file or directory in the Linux system. This is the simplest way to create a file in Linux/Unix using the terminal. 
Syntax:
The general syntax of the touch command is as follows:
$ touch [option] ... FILE...
A brief description of options available in the touch command.
In this example, using the touch command we can create a file in the Linux system. Before executing the touch command, we will check that how many files available in our current directory using the below command.
$ ls -l
After using the below command a new file created newfile.txt in the current directory.
Example : 
$ touch newfile.txt
To ensure that the file is created or not we will again execute the ls command to list the directory contents.  
The cat (concatenate) command is used to create, view, concatenate files in the Linux operating system. The touch command is also used to create a file in a Linux system without content whereas the cat creates files with some content. The cat command reads the content of a file and prompts it. 
The general syntax of the cat command is as follows:
$ cat [option]... FILE...
A brief description of options available in the cat command.
To create a file with some content, we use the cat command and file name after that write some content and press CTRL + C when writing is complete as shown below.
$ cat > file.txt
The cat command is also used to view the contents of the file. After using the cat command along with the file name contents of the file will be prompt as shown below.
In the Linux/Unix system a redirection operator is also used to create a file. 
$  > file.txt
The echo command is also used to create a new file in the Linux system. 
To create a file without contents, we use the echo command with a redirection operator followed by the file name as shown below.
$ echo > file.txt
To create a file with some contents, we use the echo command followed by the text, a redirection operator, and the file name as shown below.
heredoc stands for here document. The heredoc delimiter is a type of redirection. It allows passing multiple lines of input to a command. 
The general syntax of heredoc. Important
Command << Heredoc_delimiter
multiple lines contents...
heredoc_delimiter
To create a file using heredoc, we use the cat command with heredoc delimiter in the Linux system as shown below.
$ cat  << heredoc_delimiter < file_name
The dd command is mainly used to converts and copy files. To check more details about the dd command. We can also create a large file using the dd command.
To create a large file, we use the dd command as shown below. 
$ dd if = /dev/zero of = file.test bs =1 count =0 seek = 2G
How To
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Set Git Username and Password in GitBash?
How to Install Jupyter Notebook on MacOS?
How to Install and Use NVM on Windows?
How to Install Python Packages for AWS Lambda Layers?
How to Add External JAR File to an IntelliJ IDEA Project?
Sed Command in Linux/Unix with examples
AWK command in Unix/Linux with examples
grep command in Unix/Linux
cut command in Linux with examples
cp command in Linux with examples