Managing files from the command line is one of the most common tasks for a Linux user.

Files are created, edited, deleted, and used by many of the background OS processes. Files are also used by regular users to accomplish daily tasks such as taking notes, writing code, or simply duplicating content.

In this article, we will see three methods through which we can create files using the terminal. The three commands that we'll discuss are touch, cat and echo.

Pre-requisites:

You should have access to the Linux terminal to try out the commands mentioned in this tutorial. You can access the terminal in either of the following ways:

  • Install a Linux distro of your choice on your system.
  • Use WSL (Windows Subsystem for Linux), if you want to use Windows and Linux side by side. Here is a guide to do that.
  • Use Replit which is a browser-based IDE. You can create a Bash project and access the terminal right away.

Method #1: How to Create Files Using the touch Command

The touch command creates empty files. You can use it to create multiple files as well.

Syntax of the touch command:

 touch FILE_NAME
Syntax of the touch command

Examples of the touch command:

Let's create a single empty file using the syntax provided above.

touch access.log
Create a single file using touch

Next, we'll create multiple files by providing the file names separated with spaces after the touch command.

touch mod.log messages.log security.log
Create multiple files using touch

The above command will create three separate empty files named mod.log, messages.log, and security.log.

Method #2: How to Create Files Using the cat Command

The cat command is most commonly used to view the contents of a file. But you can also use it to create files.

Syntax of the cat command:

cat > filename
Syntax to create a file using the cat command

This will ask you to enter the text that you can save and exit by pressing ctrl+c.

cat > sample.txt

When I enter the above command, my terminal output looks like this:

zaira@Zaira:~$ cat > sample.txt
This is a sample file created using the "cat" command
^C

Note the ^C sign, which corresponds to Ctrl+c and signals to the terminal to save and exit.

Here are the contents of the created file:

zaira@Zaira:~$ more sample.txt
This is a sample file created using the "cat" command
View the contents of the file created via cat

Method #3: How to Create Files Using the echo Command

The echo command is used to add and append text to files. It also creates the file if it doesn't already exist.

Syntax of the echo command:

echo "some text" > sample.txt

or

echo "some text" >> sample.txt

The difference between > and >> is that > overwrites the file if it exists whereas >> appends to the existing file.

If you would like to follow along with the video tutorial of this article, here is the link:

Conclusion

In this article, we discussed three different methods to create files in the Linux command line. I hope you found this tutorial helpful.

What’s your favorite thing you learned from this tutorial? Let me know on Twitter!

You can read my other posts here.