How to Create a File in Linux

Updated on

6 min read

Linux Create File

If you’re coming to Linux from another operating system, such as Windows, everyday tasks like creating a new file can be challenging.

Knowing how to quickly create new files is an essential skill for anyone using Linux on a regular basis.

Linux has many command-line tools and editors that allow you to create and modify files. Linux desktop users can also create new files by right-clicking anywhere on the desktop.

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

Before you Begin

To create a new file, you need to have write permissions on the parent directory. Otherwise, you will receive a permission denied error.

Creating a File with the touch Command

The easiest and most memorable way to create new, empty files is by using the touch command.

The touch command allows us to update the timestamps on existing files and directories and create new, empty files.

To create a new file, run the touch command followed by the name of the file you want to create:

touch file1.txt

If the file file1.txt doesn’t exist, the command above will create it. Otherwise, it will change its timestamps.

To confirm that you have successfully created the file, type:

ls file1.txt

The new file should appear in the list of files in the directory.

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

touch file1.txt file2.txt file3.txt

You can also create files in another directory by providing the full path to the touch command:

touch /home/zoe/Documents/file.txt

Creating a File with the Redirection Operator

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 an existing file, while the >> operator will append the output to the file. The file will be created if it doesn’t exist.

To create an empty zero-length file, specify the name of the file 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 a redirection, be careful not to overwrite an important existing file.

Creating a File with cat Command

The cat command is mostly used to read and concatenate files, but you can also use it to create new files.

To create a new file, run the cat command followed by the redirection operator > and the name of the file you want to create:

cat > file1.txt

Press Enter, and the command will wait for you to enter a text. The cat command does not show a prompt.

Type the text, and once you are done, press the CRTL+D key sequence to save the files. To create an empty file, just press CRTL+D, without typing anything.

Creating a File with echo Command

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

To create a new file, invoke 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 an empty, run:

echo > file1.txt

Creating a File with printf Command

The printf command is another command that allows you to send the output to a file. It’s similar to echo but with more control over the formatting of the output.

To create a new file, invoke the printf command followed by the text you want to write and use the redirection operator > to write the output to the new file:

printf "Open issues: %s\nClosed issues: %s\n" "12" "25" > file.txt

Creating a File using Heredoc

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

This method is mostly used when creating a file containing multiple lines of text from a shell script.

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

cat << EOF > file1.txtSome lineSome other lineEOF

The body of the Heredoc can contain variables, special characters, and commands.

Creating a File with a Text Editor

Each Linux distribution comes with one or more text editors installed by default. The two most commonly used text editors are Vim and Nano .

Creating a File using Nano

Nano is an easy-to-use command line text editor for Unix and Linux operating systems.

To create a new file, invoke the editor nano followed by the file name:

nano file.txt

A new editor window will open in which you can enter your text.

nano create file

When you’re done typing and want to exit Nano , press the key combination Ctrl-x.

The editor will prompt you whether you want to save the changes or not. Type y for yes. Accept the name of the new file and press Enter to save it.

Creating a File using Vim/Vi

Vim or its precursor Vi comes preinstalled on almost all Linux distributions.

To create a file using Vim, in your terminal, type vim or vi followed by the file name:

vim file.txt

Vim has several modes of operation, which can be a little intimidating for new users. To be able to type text, press the i key to switch to insert mode.

vim create file

Once done typing, to save the file and exit the editor, press Esc to switch to normal mode, type :wq, and hit Enter.

Creating a Large File

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

Using dd command

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

To create a file named 1G.test with a size of 1GB, you would run the following:

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

Using fallocate command

fallocate a command-line utility for allocating real disk space for files.

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

fallocate -l 1G 1G.test


We have shown you how to create a new file in Linux from the command line using different commands, editors, and redirection.

If the command line is not your thing, you can easily create a blank text file using the right-click menu in the File Manager.

If you have questions, feel free to leave a comment below.