Linux Commands Ubuntu

How to Create a Blank File in Linux/Ubuntu?

How to Create a Blank File in Linux Ubuntu

Files are used extensively to perform daily tasks like writing code, taking notes, etc. Files can be manipulated, i.e., modified, created, and deleted by the processes of the Operating System (OS) as well as users. Blank Files are files that are empty, i.e., they contain no data and are of zero bytes. Linux/Ubuntu’s Terminal can be used to create blank files by different commands.

This article will demonstrate different ways of creating a blank file in Linux/Ubuntu 22.04 LTS, i.e., via

  • Touch Command.
  • Cat Command.
  • Echo Command.
  • Redirection Operator.
  • Printf Command.

How to Create/Generate a Blank File in Linux/Ubuntu via “touch” Command?

The touch command creates blank files. A blank file is created via touch command by the following syntax:

$ touch <Filename>

Using the above syntax, a blank file “File1.txt” is created by:

$ touch File1.txt

From the above image, it can be verified that the new file: File1.txt has been created and it is of zero bytes. File size can also be verified by stat command as follows:

$ stat File1.txt

From the above image, it can be seen that the size of File1.txt is 0 bytes, i.e., File1.txt is a blank file.

How to Create/Generate a Blank File in Linux/Ubuntu via the “cat” Command?

The cat (concatenate) command is typically utilized to view the contents of the file. Additionally, it can also be used to create a file. A blank file is created via the cat command by the following syntax:

$ cat > <Filename>

Using the above syntax, a blank file “File2.txt” is created by:

$ cat > File2.txt

There will be a prompt to enter text, but as we are creating a blank file, a combination of [Ctrl+C] is pressed to save and exit the file. Additionally, from the above image, it can be verified that the new file: File2.txt has been created and it is of zero bytes. File size can also be verified by stat command as follows:

$ stat File2.txt

From the above image, it can be seen that the size of File2.txt is 0 bytes, i.e., File2.txt is a blank file.

How to Create/Generate a Blank File in Linux/Ubuntu via “echo” Command?

The echo command is mainly used to append or overwrite text files. However, if the file doesn’t exist, the echo can be also used to create a file. A blank file is created via echo command by the following syntax:

$ echo -n > <Filename>

Where,

  • -n: do not output the trailing newline.
  • >: Overwrite the existing file if <filename> exists.
  • >>: Append the existing file if <filename> exists.

Using the above syntax, a blank file “File3.txt” is created by:

$ echo -n > File3.txt

From the above image, it can be verified that the new file: File3.txt has been created and it is of zero bytes. File size can also be verified by stat command as follows:

$ stat File3.txt

From the above image, it can be seen that the size of File3.txt is 0 bytes, i.e., File3.txt is a blank file.

How to Create/Generate a Blank File in Linux/Ubuntu via Redirection Operator?

A redirection operator is mainly used to append or overwrite text files but if a file does not exist, the redirection operator creates a new file. A blank file is created via the redirection operator by the following syntax:

$ > <Filename>

Where,

  • >: Overwrite the existing file if <filename> exists.
  • >>: Append the existing file if <filename> exists.

Using the above syntax, a blank file “File4.txt” is created by:

$ > File4.txt

From the above image, it can be verified that the new file: File4.txt has been created and it is of zero bytes. File size can also be verified by stat command as follows:

$ stat File4.txt

From the above image, it can be seen that the size of File4.txt is 0 bytes, i.e., File4.txt is a blank file.

How to Create/Generate a Blank File in Linux/Ubuntu via Printf Command?

The printf command is used to create a new file along with displaying the data in the file. A blank file is created via printf command by the following syntax:

$ printf > <Filename>

Using the above syntax, a blank file “File5.txt” is created by:

$ printf '' > File5.txt

In the above command, single quotes (‘ ’) are used in order to create an empty file. If any characters were inserted between single quotes (‘ ’), then that data would be written in the file. Additionally, from the above image, it can be verified that the new file: File5.txt has been created and it is of zero bytes. File size can also be verified by stat command as follows:

$ stat File5.txt

From the above image, it can be seen that the size of File5.txt is 0 bytes, i.e., File5.txt is a blank file.

Conclusion

Blank Files are created by different methods in Linux/Ubuntu, like touch, cat, echo, redirection operator, and printf command. Additionally, the file creation can be verified by either “ls -l” or by the “stat <file_name>” command. These commands information about files. This article demonstrated five different methods of creating a blank file in Linux/Ubuntu 22.04 LTS.

Similar Posts