How to Create a File in Linux/Ubuntu from the Command Line?


Files are a crucial component of any operating system that assists us in organizing and storing data efficiently. Files are created by users as well as by operating systems (OS) processes. A Linux/Ubuntu user manages many files as part of a daily task, for example, to write code, create scripts, write notes, etc. Files can be created via the Command Line Interface (CLI) as well as by Graphic User Interface (GUI).

There are several ways to create a file via Linux/Ubuntu CLI which will be demonstrated in this article. This article is organized as follows:

  • How to Create/Make a File via Touch Command in Linux/Ubuntu?
  • How to Create/Make a File via Redirection Operator (>) in Linux/Ubuntu?
  • How to Create/Make a File via Printf Command in Linux/Ubuntu?
  • How to Create/Make a File via Echo Command in Linux/Ubuntu?
  • How to Create/Make a File via Cat Command in Linux/Ubuntu?
  • How to Create/Make a File via Fallocate Command in Linux/Ubuntu?
  • How to Create/Make a File via dd Command in Linux/Ubuntu?
  • How to Create/Make a File via Nano Text Editor in Linux/Ubuntu?
  • How to Create/Make a File via Vi Text Editor in Linux/Ubuntu?

Let’s begin with creating a file via the touch command.

How to Create/Make a File via Touch Command in Linux/Ubuntu?

The touch command is utilized frequently to create a file in Linux/Ubuntu. To create a file, File1.txt via touch command, execute the following command:


The above output verifies the successful creation of a new file: File1.txt.

How to Create/Make a File via Redirection Operator (>) in Linux/Ubuntu?

A redirection operator (>) is used to create, overwrite, and append files in Linux/Ubuntu. A redirection operator only creates a new file if a file does not exist. To create a file, File2.txt via the redirection operator (>) command, execute the following command:


The above output verifies the successful creation of a new file: File2.txt. If File2.txt already exists, it will be over-written by the above command. Additionally, >> can be used instead of > to append an existing file.

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

The printf command is used to append and create a file in Linux/Ubuntu. To create a file, File3.txt via printf command, execute the following command:

$ printf ‘Test file created via printf Command’ > File3.txt

The above output verifies the successful creation of a new file: File3.txt. If File3.txt already exists, it will be over-written by the above command. Additionally, >> can be used instead of > to append an existing file.

 

How to Create/Make a File via Echo Command in Linux/Ubuntu?

The echo command depicts text to standard output in Linux/Ubuntu. Additionally, the echo command appends text and overwrites files. To create a file, File4.txt via echo command, execute the following command:

$ echo “Test file created via echo command” > File4.txt

The above output verifies the successful creation of a new file: File4.txt. If File4.txt already exists, it will be over-written by the above command. Additionally, >> can be used instead of > to append an existing file.

How to Create/Make a File via Cat Command in Linux/Ubuntu?

The cat (concatenate) is mainly used to read files. Additionally, it is used to create, append, and overwrite existing files. To create a file, File5.txt via cat command, execute the following command:


A user can enter text at the prompt. If a file, File5.txt doesn’t exist already, a new file, File5.txt will be created by containing the contents that the user typed earlier. Finally, press Ctrl+C to save the text file. The above output verifies the successful creation of a new file: File5.txt. If File5.txt already exists, it will be over-written by the above command. Additionally, >> can be used instead of > to append an existing file.

How to Create/Make a File via Fallocate Command in Linux/Ubuntu?

The fallocate command is typically used to create files of the desired size by pre-allocating space to a file in Linux/Ubuntu. To create a file, File8.txt of size 1MB via fallocate command, execute the following command:

$ fallocate -l 1M File8.txt

Where the “-l” flag is the length parameter.

The above output verifies the successful creation of a new file: File8.txt.

How to Create/Make a File via dd Command in Linux/Ubuntu?

The dd command is utilized to create a file of the desired size in Linux/Ubuntu. To create a file, File9.txt via dd command, execute the following command:

$ dd if=/dev/zero of=File9.txt bs=1 count=0 seek=1M

Where:

  • /dev/zero: represents an empty file.
  • of: writes a file.
  • bs: represents bytes to read and write simultaneously.
  • count: represents the number of blocks to be copied
  • seek: represents the size of the file.

The above output verifies the successful creation of a new file: File9.txt.

How to Create/Make a File via Nano Text Editor in Linux/Ubuntu?

Nano is a powerful command line-based text editor for Unix-like systems. It comes pre-installed in Linux/Ubuntu systems. Nano text editor contains pre-defined shortcut keys for effective file handling. To create a file, File6.txt via the Nano text editor, execute the following command to launch the Nano text editor:


Type the contents of the file in the editor. Save and Close the file by pressing Ctrl+O and Ctrl+X respectively. To verify the creation of the file, execute the following list command:

The above output verifies the successful creation of a new file: File6.txt.

How to Create/Make a File via Vi Text Editor in Linux/Ubuntu?

Vi is a free-to-use command line-based text editor for Linux systems. It is installed by default in Linux/Ubuntu systems. To create a file, File7.txt via the Vi text editor, execute the following command to launch Vi editor:


To enter text, first, enable insert mode by pressing “i”. Then, type the desired text in the editor. Next, to save and exit the file, press “Esc” to enable command mode and type “:wq”:


To verify the creation of the file, execute the following list command:

The above output verifies the successful creation of a new file: File7.txt.

Conclusion

In Linux/Ubuntu, files are created by many different methods via the command line such as touch, printf, echo, cat, and via the redirection operator. Additionally, command line-based editors like nano and vi text editors can be used to create. Moreover, a file of a specific size can be created via fallocate and dd commands. While creating a file via commands, a user has a limit to add text. On the other hand, a user can enter large text as well as edit text while creating files via text editor. This article demonstrated nine different ways of creating a file via the command line in Linux/Ubuntu systems.

Print Friendly, PDF & Email