How to Create a File in the Linux Using the Terminal?
Last Updated :
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 command
- Using the cat command
- Using redirection operator
- Using the echo command
- Using the heredoc
- 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

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

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.

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

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

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.

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

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
Similar Reads
How to Create a Text File Using the Command Line in Linux
The Linux command line is a powerful tool that allows you to manage your system with precision and speed. One of the most common tasks is creating a text file, which can be achieved using several commands and methods. Here we will learn some quick ways to create a text file from the Linux Command Li
6 min read
How to Create a File in VSCode using Terminal?
In a few cases touch or new-item or any other base keywords may not be used to create a file in vs code due to the absence of extensions, but using the following approach you can create and code any type of file with ease. In this article, you will learn about how to create a file with any file type
3 min read
How to Clear the Terminal History in Linux
The Linux Terminal stores every command you execute in a history file like .bash_history for Bash or .zsh_history for Zsh, making it convenient to access previous commands. However, this history can become a privacy or security issue, especially when dealing with sensitive tasks or working on shared
6 min read
How to Create a VIM File in Linux CMD
In Linux, there are many ways to open and create files, but Vim stands out as a powerful and versatile text editor. It comes pre-installed on most Linux systems and allows you to create, edit, and manage files directly from the terminal. In this article, we will explain you, how to create a new file
3 min read
How to Create a Shell Script in linux
Shell is an interface of the operating system. It accepts commands from users and interprets them to the operating system. If you want to run a bunch of commands together, you can do so by creating a shell script. Shell scripts are very useful if you need to do a task routinely, like taking a backup
7 min read
How to Create File in Linux
Today, we're going to learn about something really important â how to create files in Linux. It's like creating a fresh piece of digital paper to write or store things. We'll explore different ways to do this using simple commands. Whether you're just starting out or have been using Linux for a bit,
7 min read
How to Create a Swap File on Linux?
Linux Operating System has some different features that are unique and not present in any other major operating system like Windows. The Linux Memory Distribution is one of them. The Memory Space in Linux is designed in such a way that the efficiency of the Memory Management could be the best. The L
4 min read
How to Find Linux File Creation Time using Debugfs?
Everything is treated as a file in Linux, and all the information about a file is stored in inodes, which includes the crucial metadata about a file such as creation time, last modification, etc. Every file in Linux is identified by its inode number. In this article, we will be using debugf command
2 min read
How to Set File Permissions in Linux?
Ever worried about who can access, modify, or execute your critical files on a Linux system? File permissions are the backbone of Linux security, ensuring that only authorized users and processes interact with your data. In this guide, youâll learn how to master Linux file permissions using commands
10 min read
How to View the Content of File in Linux | cat Command
The cat command in Linux is more than just a simple tool, it's a versatile companion for various file-related operations, allowing users to view, concatenate, create, copy, merge, and manipulate file contents. Let's see the details of some frequently used cat commands, understanding each example alo
7 min read