How to create a file in Linux using a terminal

As we all know, Linux is an operating system mainly used by Geeks and developers who mainly work on the keyboard 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 of the mouse, while on Linux we have commands for everything, for example, for working with main files, compressing or extracting files, etc. These commands run on a Linux command line, known as Terminal or Shell, A terminal or shell is a utility in Linux that is responsible for running commands. Today I am going to share various methods with which you can create a file in Linux through the terminal.

Introduction

In a world where a lot of work has already been done to improve the UI / UX, and the user interface is so intuitive and powerful, however, the command line has many advantages over the GUI. Typically, Linux is an operating system used on servers, and is mainly used by technical users, such as Developers. Their key requirement is that they require more control over the system, require high performance, scripting capabilities and much more, which, unfortunately, the GUI cannot provide. Mostly in server environments, such as data centers, we don’t have a graphical interface installed on the servers, because the graphical interface loads a lot of time and is intended mainly for end users. Thus, to be a good technical user, you need to have good command over the Shell a.k.a terminal.

Here are a few of the benefits of the command line interface over graphical user interfaces:

  • Control over the system.
  • Make it easy to perform many tasks, such as renaming thousands of files.
  • Ability to use scripts.
  • Less memory and faster performance.

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

Create a file in the Linux shell

On Linux, there are many commands with which the user can create files. Each team has its own meaning. Some of the most used are:

1. Using the “cat” command.2. Using the “touch” command.3. Using the символ> ’redirection symbol

We are going to discuss them one by one.

For this tutorial, I am using the Ubuntu version of the Linux operating system. Thus, the screenshot for the demo purpose will be based on Ubuntu.

1. Using the cat command

The cat command, also known as the concatenate command, is one of the most commonly used commands on Linux. There are several functions of the “cat” command, which includes

  • Create one or more files.
  • View file contents on command line
  • Redirect the output of one file to the terminal screen or to another file

However, in this lesson we focus only on creating the file. So, let’s see how we can create a file using the “cat” command.

Step 1: First of all, open the terminal by clicking on Ubuntu Launcher and find the terminal.

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

Step 3: By default, the terminal is in the “home” location, but to check where the terminal is pointing right now, we will use the “pwd” command. “Pwd” will return the path that the terminal is currently pointing to. Right now we are creating the file in the default location that the terminal points to, but if you want to create the file elsewhere, you can change the path using the “cd” change directory command. The general syntax for the cd command is “cd“ folder path ”.

Step 4: Now, to create the file, write the command “cat> filename.ext”, where filename will be the name of your file and ext will be the file extension. For instance. in the demo i use dummy.txt

Step 5: After executing the command in the default path, a text file is created with the name you specified. In our case, this is the dummy.txt file

Now you can see that the cursor is blinking, waiting for input from the user. Essentially, the command asks for the text you want to write to the file. If you want to leave the file empty, simply press “Ctrl + D” or, if you want to write the contents to a file, enter it, and then press “Ctrl + D”. The content has been saved to a file and you will return to the main terminal.

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

Shell-generated text file

Congratulations! Your file was created using the “cat” command.

Remarks: Before creating a new file, make sure that the file has not yet been created. You can use the ls command for this.

2. Using the touch command

In the Linux operating system, each file has timestamp information, for example, the last time a file was accessed or modified, etc. Each time a file is accessed or modified, this timestamp is updated. The touch command is a Linux utility that is used to create, modify, or change the timestamp of a file.

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

Step 1: First of all, open the terminal by clicking on Ubuntu Launcher and find the terminal.

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

Step 3: By default, the terminal is in the “home” location, but to check where the terminal is pointing right now, we will use the “pwd” command. “Pwd” will return the path that the terminal is currently pointing to. Right now we are creating the file in the default location that the terminal points to, but if you want to create the file elsewhere, you can change the path using the “cd” change directory command. The general syntax for the cd command is “cd“ folder path ”.

Step 4: Now, to create the file, write the command “touch filename.ext”, where filename will be the name of your file and ext will be the file extension. For instance. in the demo I use dummy.txt. After executing the command, the terminal will create a file along the path, as shown in the following screenshots:

Create a file using the cat command

Congratulations! Your file was created using the “touch” command.

Note. Before creating a new file, make sure that the file has not yet been created. You can use the ls command for this.

3. Using the Redirect> operator

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

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

Step 1: First of all, open the terminal by clicking on Ubuntu Launcher and find the terminal.

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

Step 3: By default, the terminal is in the “home” place, but to check where the terminal is pointing right now, we will use the “pwd” command. “Pwd” will return the path that the terminal is currently pointing to. Right now we are creating the file in the default location that the terminal points to, but if you want to create the file elsewhere, you can change the path using the “cd” change directory command. The general syntax for the cd command is “cd” folder path “.

Step 4: Now, to create the file, write the command “echo“ This is dummy text ”> filename.ext”, where filename will be the name of your file and ext will be the file extension. For instance. in the demo I use dummy.txt. After executing the command, the terminal will create a file along the path, as shown in the following screenshot:

Using the redirect operator to create a file

Conclusion

In this guide, we discussed the need for a command line interface, its advantages, and various methods for creating a file in Linux using a terminal.

How to create a file in Linux using a terminal

Sidebar