How to Create a File in Linux Using Terminal

Createe files on Linux shell

As we all know, Linux is an operating system mainly used by geeks and developers, who are mostly keyboard people and like to write commands instead of using a graphical user interface (GUI). Unlike the Windows operating system, where most of the work is done with a few clicks, Linux has commands for everything, such as basic file manipulation, compression or extraction of files, etc. These commands run on the Linux command line known as the terminal or shell. The terminal or shell is a utility in Linux that is responsible for running the commands. Today, I will introduce various methods that you can use to create a file in Linux using the terminal.

Introduction

In a world where a lot of work has already been put into improving UI/UX and the UI is so much more intuitive and powerful, the command line still has a lot of advantages over the GUI. Typically, Linux is an operating system used in servers and mostly used by technical users, such as developers. Their main requirement is to have more control over the system, need fast performance, scripting capabilities, and much more, which the GUI, unfortunately, cannot provide. In server environments such as data centers, we usually don't have GUI installed on the servers because the GUI takes a lot of time to load and is basically meant for the end-users. So to be a good technical user, you should have a good command of the shell, also known as the terminal.

Here a few advantages of a command-line interface over graphical user interfaces:

  • Control over the system.
  • Provide ease for many tasks like rename thousands of files in bulk.
  • The ability to use scripts.
  • Less memory and faster performance.

Now I am going to share different methods through which you can create a file in Linux.

Create a file on Linux shell

In Linux, there are a lot of commands through which a user can create files. Each command has its own significance. Some of the most used are:

1. Using ‘cat’ command.
2. Using ‘touch’ command.
3. Using redirect ‘>’ symbol

We are going to discuss them one by one.

For this tutorial, I am using the Ubuntu flavor of the Linux Operating system. So the screenshot for the demo purpose will be based on Ubuntu. But all commands are available on other Linux Distributions like Debian and CentOS too.

1. Using cat command

The ‘cat’ command also known as “concatenate” command is one the most frequently used command in Linux OS. There are multiple functionalities of the ‘cat’ command which includes

  • Creation of single or multiple files.
  • View the content of the file on the command line
  • Redirect output of one file on the terminal screen or in another file

However, in this tutorial, we are only focused on the creation of a file. So, let’s see how we can create a file by using ‘cat’ command.

Step 1: First of all, open Terminal by clicking on Ubuntu launcher and search for Terminal.

Step 2: Now click on the Terminal and wait for the terminal to open.

Step 3: By default, the terminal is on the ‘home’ location but to verify where the terminal is pointing right now we will use ‘pwd’ command. The ‘pwd’ will return the path to which the terminal is currently pointing. Right now, we are creating a file on the default location to which a terminal is pointing but if you want to create a file in some different location you can change the path by using ‘cd’ change directory command. The general syntax of cd command is “cd ‘path to folder’”.

Step 4: Now to create a file write a command “cat > filename.ext” where filename will be the name of your file and ext will be an extension of the file. E.g. in the demo, I’m using dummy.txt

Step 5: Once the command is executed, a text file is created in the default path with the name you have provided. In our case it is the file dummy.txt

Now you can see the cursor is blinking awaits input from the user. Basically, the command is asking to type the desired text you want to write to a file. If you want to keep the file empty just press “ctrl+D” or if you want to write the content to the file, type it and then press “ctrl+D”. The content has been saved to the file and you will be returned to the main terminal.

You can verify the text by opening the file as shown in the screenshot.

Text file created on the shell

Congratulations! Your file has been created with ‘cat’ command.

Note: Before creating a new file make sure that the file is not already created. To ensure this you can use “ls” command.

2. Using touch command

In Linux operating system, every file has timestamp details like the last time the file is accessed or modified etc. Every time the file is accessed or modified this timestamp is updated. The ‘touch’ command is a utility of Linux which is used to create, change or modify the timestamp of the file.

Let’s see how we can create a file by using ‘touch’ command.

Step 1: First of all, open Terminal by clicking on Ubuntu launcher and search for Terminal.

Step 2: Now click on the Terminal and wait for the terminal to open.

Step 3: By default, the terminal is on the ‘home’ location but to verify where the terminal is pointing right now we will use ‘pwd’ command. The ‘pwd’ will return the path to which the terminal is currently pointing. Right now, we are creating a file on the default location to which a terminal is pointing but if you want to create a file in some different location you can change the path by using ‘cd’ change directory command. The general syntax of the cd command is “cd ‘path to folder’”.

Step 4: Now to create a file write a command “touch filename.ext” where filename will be the name of your file and ext will be the extension of the file. E.g. in the demo I’m using dummy.txt. Once the command is executed the terminal will create a file on the path as shown in the following screenshots:

Create a file with cat command

Congratulations! Your file has been created with ‘touch’ command.

Note: Before creating a new file make sure that the file is not already created. To ensure
this you can use “ls” command.

3. Using Redirect > operator

In Linux ‘>’ is known as output redirection operator provides an option to redirect the output of the command to a file instead of the standard terminal screen. We can also use the redirect operator to create a file.

let’s see how we can create a file by using ‘touch’ command.

Step 1: First of all, open Terminal by clicking on Ubuntu launcher and search for Terminal.

Step 2: Now click on the Terminal and wait for the terminal to open.

Step 3: By default, the terminal is on the ‘home’ location but to verify where the terminal is pointing right now we will use ‘pwd’ command. The ‘pwd’ will return the path to which the terminal is currently pointing. Right now, we are creating a file on the default location to which a terminal is pointing but if you want to create a file in some different location you can change the path by using ‘cd’ change directory command. The general syntax of cd command is “cd ‘path to folder’”.

Step 4: Now to create a file write a command “echo “this is a dummy text” > filename.ext” where filename will be the name of your file and ext will be the extension of the file. E.g. in
the demo I’m using dummy.txt. Once the command is executed the terminal will create a file on the path as shown in the following screenshot:

Using redirect operator to create a file

Conclusion

In this tutorial, we have discussed the need for a command-line interface, its advantages, and the different methods to create a file in Linux by using the terminal.