Open In App

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

Last Updated : 09 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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. 

  1. Using the touch command
  2. Using the cat command
  3. Using redirection operator
  4. Using the echo command
  5. Using the heredoc
  6. Using the dd command

1. Create a file in the Linux/Unix system using the touch 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.

Option Description
-a Change the access time of a file
-c, –no-create Check file is available or not, if not available then prevent creating a file 
-f ignored
-m Change the modification time of a file
-t STAMP Use specified time instead of the current time
–help Display help and exit
–version Display the version information and exit.

Create a file using the touch command in Linux/Unix system.  

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

How to create a file in the Linux system using  the terminal?

To ensure that the file is created or not we will again execute the ls command to list the directory contents.  

 2. Create a File in the Linux/Unix system using the cat command.

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. 

Syntax

The general syntax of the cat command is as follows:

$ cat [option]... FILE...

A brief description of options available in the cat command.

Option Description 
-A, –show-all Show all content of a file
-b, –number-nonblank Display number of non-empty lines overrides -n
-n, –number Display number of all output lines
-T, –show-tabs Display this help and exit
–help Display this help and exit
–version Display version information and exit

Create a file with some content using the cat command in Linux/Unix system.

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.

Example :

$ cat > file.txt

How to create a file in the Linux system using  the terminal?

Display contents of the files using the cat command in the Linux system.

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.

How to create a file in the Linux system using  the terminal?

3. Create a file in the Linux/Unix system using a redirection operator. 

In the Linux/Unix system a redirection operator is also used to create a file. 

Example :

$  > file.txt

How to create a file in the Linux system using  the terminal?

4. Create a file in the Linux/Unix system using the echo command.

The echo command is also used to create a new file in the Linux system. 

Create a new file without contents in the Linux system using the echo command. 

To create a file without contents, we use the echo command with a redirection operator followed by the file name as shown below.

Example :

$ echo > file.txt

How to create a file in the Linux system using  the terminal?

Create a new file with some contents in the Linux system using the echo command. 

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.

How to create a file in the Linux system using  the terminal?

5. Create a file in the Linux/Unix system using heredoc.

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

Create a file with multiple lines of contents using a heredoc delimiter in the Linux system. 

To create a file using heredoc, we use the cat command with heredoc delimiter in the Linux system as shown below.

Example :

$ cat  << heredoc_delimiter < file_name

How to create a file in the Linux system using  the terminal?

6. Create a file in the Linux/Unix system using the dd command.

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.

Create a large file in the Linux system using the dd command. 

To create a large file, we use the dd command as shown below. 

Example :

$ dd if = /dev/zero of = file.test bs =1 count =0 seek = 2G

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads