Createe files on Linux shell

As we all know Linux is an operating system mostly used by the Geeks and developers which are mostly a keyboard person and love to write commands instead of using Graphical user Interface (GUI). Unlike Windows operating system, where most of the work done is with few clicks while in Linux we have commands for everything like basic file manipulations, compression or extraction of files etc. These commands run on the Linux command line which is known as Terminal or Shell. The Terminal or shell is a utility in Linux which is responsible to run the commands. Today I am going to share different methods through which you can create a file in Linux via terminal.

Introduction

In a world where already a lot of work has been done in improving UI/UX and the UI is so much intuitive and powerful, still, command line has a lot of advantages over GUI. Typically, Linux is an operating system used in servers and is mostly used by the technical users e.g. developers. Their key requirement is they require more control over the system, require fast performance, scripting capability and much more which unfortunately GUI is not able to provide. Mostly in server environments like data centers we do not have any GUI installed on the servers because GUI takes lot of time to load and is basically for the end users. So, in order to be a good technical user, one should have good command over Shell a.k.a 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 Ubuntu flavor of Linux Operating system. So the screenshot for the demo purpose will be based on Ubuntu.

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 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 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 a 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 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.

How to Create a File in Linux Using Terminal