How to Create an Empty File in Linux

How to Create an Empty File in Linux

An empty file has zero byte size and it is a plain text file. In this guide, we will look into the following three ways you can use to create an empty file in Linux.

  • Using touch command
  • Using echo command
  • Using printf command

Note: I have implemented the commands on Red Hat Enterprise Linux 9.

Using touch command

We will discuss the first method of creating an empty file which is the touch command. The command has following syntax.

touch <filename>

Suppose, you want to create an empty file sample.txt in your current working directory. Execute the following on terminal.

touch sample.txt

You can confirm the file creation with the help of ls command.

ls

Using echo command

This is the second command we can use to create an empty file. Execute the following echo command with -n option.

echo -n > sample.txt

You can confirm the file creation by listing the content of your current working directory.

ls

Using printf command

Alternatively, you can create an empty file by using printf command on the terminal. Suppose we want to create a file called sample.txt, the command has following syntax.

Printf ‘ ’ > sample.txt

Confirm the file by executing ls on the terminal.

ls

Conclusion

We have successfully created the file by using touch, echo, and printf commands.