On Linux, we can create a new file in the terminal, or from the file manager on the desktop.

Knowing how to create a new file is an important skill for anyone who regularly uses Linux.

In this tutorial, we will show you various ways to quickly create a new file in Linux using the command line.

Before you start
To create a new file, you must have write permissions to the parent directory. Otherwise, you will receive an “Access Denied” error.

If you want to display the contents of the directory, use the ls command.

Creating a file using the touch command

The touch command allows you to update timestamps on existing files and directories, as well as create new, empty files.

The easiest and most memorable way to create new empty files is to use the touch command.
To create a new file, simply run the command with the name of the file you want to create:

touch file1.txt

If file1.txt does not exist, the command above will create it, otherwise it will change its timestamps.

To create multiple files at once, specify the file names separated by spaces:

touch file1.txt file2.txt file3.txt

Creating a redirect statement file

Redirection allows you to capture the output from a command and send it as input to another command or file. There are two ways to redirect output to a file. The “>” operator will overwrite the existing file, while the “>>” operator will add output to the file.

To create an empty file of zero length, simply specify the name of the file that you want to create after the redirection operator:

> file1.txt

This is the shortest command to create a new file in Linux.

When creating a file using redirection, be careful not to overwrite an important existing file.

Creating a file with the cat command

The cat command is mainly used to read and merge files, but can also be used to create new files.

To create a new file, run the cat command and then the redirection operator “>” and the name of the file you want to create. Press Enter, enter the text, and as soon as you are done, press CRTL + D to save the files.

cat> file1.txt

Creating a File Using the echo Command

The echo command prints strings that are passed as arguments to standard output, which can be redirected to a file.

To create a new file, run the echo command, followed by the text you want to print, and use the redirection operator, “>” to write the output to the file you want to create.

echo "Some line"> file1.txt

If you want to create empty just use:

echo> file1.txt

Creating a file using Heredoc

Heredoc is a type of redirection that allows you to pass multiple lines of input to a command.

This method is mainly used when you want to create a file containing several lines of text from a shell script.

For example, to create a new file, file1.txt, you must use the following code:

<< EOF> file1.txt
Some line
Some other line
Eof

The heredoc body may contain variables, special characters, and commands.

Create a large file

Sometimes, for testing purposes, you may want to create a large data file. This is useful when you want to check the write speed of your disk or check the download speed of your connection.

Using the dd command

The dd command is mainly used to convert and copy files.

To create a file called 1G.test 1 GB in size, you must run:

dd if=/dev/zero of=1G.test bs=1 count=0 seek=1G

Using the fallocate command

fallocate command line utility to allocate real disk space for files.

The following command will create a new file named 1G.test with a size of 1 GB:

fallocate -l 1G 1G.test

Conclusion
In this guide, you learned how to create a new file in Linux from the command line using various commands and redirection.

If the command line is not your thing, you can easily create an empty text file using the context menu in the file manager.

If you have any questions, feel free to leave comments below.